From: Robert Klemme Date: 2008-10-21T05:28:47+09:00 Subject: Re: Threading best practices? On 20.10.2008 21:42, Dolazy wrote: > I am developing a small GUI application using jruby. It will be used > for testing API calls to my company's server. I would like my > application to execute server calls in a non-blocking way. What does the rest of the application do in the meantime? > How do I do this in Ruby? > > My simplified code looks something like this: > > def get_users > Thread.new(server, successCallback, errorCallback) do |server, scb, > ecb| > res = Net::HTTP.post_form(server, { "action" => "getUsers"}) > if res.body > scb.call(res.body) # => this should be wrapped in some sort of > InvokeLater ? > else > ecb.call("getUsers failed") # => this too > end > end > end > > I would prefer to use the Ruby standard library over the Java library. Not sure what exactly you mean by "InvokeLater". Do you want to pass the result of method calls back to the calling thread? Or do you want to asynchronously invoke callbacks? One thing which might be useful for you is Thread#value which joins a thread and returns the last value: robert@fussel ~ $ ruby -e 'puts Time.now;t=Thread.new {sleep 1; 123};puts t.value, Time.now' Mon Oct 20 22:28:28 +0200 2008 123 Mon Oct 20 22:28:29 +0200 2008 robert@fussel ~ $ Another option is to set up a Queue for feedback which is queried from the main thread. It all depends on what you application does or what you want to achieve. Can you give more detail? Kind regards robert