# Threading best practices?

**URL:** <https://www.ruby-forum.com/t/threading-best-practices/151383>\
**Category:** Ruby\
**Created:** [October 20, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/threading-best-practices/151383 "2008-10-20T21:45:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 20, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/1 "2008-10-20T21:45:33Z")

</div>

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. In C++ I do  
this by creating a worker thread that executes the call and then  
invokes a postback to the main thread (similar to Java’s InvokeLater).

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.

Eager to learn,  
Francis

---

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 20, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/2 "2008-10-20T22:30:55Z")

</div>

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?

> 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

---

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 20, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/3 "2008-10-20T23:16:08Z")

</div>

On 20 okt, 22:29, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?  
> Anything that the user is doing at that time. I want to avoid freezing  
> the GUI.

> Not sure what exactly you mean by “InvokeLater”. Do you want to pass  
> the result of method calls back to the calling thread?

Yes, that’s exactly what I have in mind.

> 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?

For example, one of the first calls will be the login method. In a  
worker thread I want to send the login message, receive the response,  
and then notify the main thread of either a successful or failed  
login, by means of invoking the corresponding callback.

---

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 21, 2008, 2:56am UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/4 "2008-10-21T02:56:42Z")

</div>

Dolazy wrote:

> the GUI.  
> For example, one of the first calls will be the login method. In a  
> worker thread I want to send the login message, receive the response,  
> and then notify the main thread of either a successful or failed  
> login, by means of invoking the corresponding callback.

I’m going to out on a limb and assume you’re using Swing here. To keep  
the  
GUI painting properly and new events being processed you have to make  
sure  
the EDT (event dispatch thread) does not block. So in your event  
handler  
you need to spin off a new thread and return as fast as possible to  
prevent  
“laggyness” in the UI. This does mean you need some asynchronous way of  
getting your response, a queue is probably your best bet here (plus it’s  
thread safe). In Monkeybars, we used a library named Foxtrot (Java lib)  
that processed GUI redraw events (but not new actions) while an action  
was  
being performed. For short-lived processes this worked great and  
prevented  
the mess of having to get data back from an asynchronous thread.

David K.

---

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 21, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/5 "2008-10-21T23:45:50Z")

</div>

> David K.

Yeah I started using MonkeyBars. I find it a very interesting project,  
but complicated to learn (may also since the tutorial videos don’t  
seem to work 😉 I’m gonna stick to it though because it combines so  
much great stuff (Ruby style, Swing power, “neo”-MVC, neat folder  
structure, deployment, …)

---

<div class="post-metadata">

**Author:** ![Dolazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dolazy](https://www.ruby-forum.com/u/Dolazy)\
**Post date:** [October 22, 2008, 12:42am UTC](https://www.ruby-forum.com/t/threading-best-practices/151383/6 "2008-10-22T00:42:41Z")

</div>

Dolazy wrote:

> Yeah I started using MonkeyBars. I find it a very interesting project,  
> but complicated to learn (may also since the tutorial videos don’t  
> seem to work 😉 I’m gonna stick to it though because it combines so  
> much great stuff (Ruby style, Swing power, “neo”-MVC, neat folder  
> structure, deployment, …)

Much apologies for the tutorial videos not working. We did a bit of  
server  
shuffling a while back and apparently I never got the video files back  
online. I have fixed this and the last few files will be online within  
a  
half hour. Hopefully that will help a lot with the “getting started”  
business.

David K.
