We have ensured that the longRunningMethod takes more than 1sec(5sec)
for execution using nested for loops
As per our understanding, the shortRunningMethod should be completed
first and then longRunningMethod.
However we observed this only when we did not have any sleep command.
But when we introduced the “sleep 1” statement. The longRunningMethod
gets executed first and then shortRunningMethod
Anyone would give us the pointers as to why the sleep statement produces
this result?
On Tue, Sep 6, 2011 at 10:11 AM, Balasaheb S. [email protected]
wrote:
test.rb
t1.join
Anyone would give us the pointers as to why the sleep statement produces
this result?
[We are using ruby 1.8.6] Thanks in advance.
1.8.6 has green threads and does not use native threads. This means
there is no real parallel execution. It may be that the sleep
triggers Ruby’s scheduler to pick another thread which happens to be
stuck in the long C function call. If you think about it it’s a
reasonable approach because usually any other thread than the sleepy
thread will have to do work to do.
Behavior may be different on 1.9.* since that uses native threads.
However, I would not yet rely on real thread concurrency. For that
you need JRuby. See this test on my machine (4 cores):