Thread.new do
5.times do |time|
link = Thread.new do
p time
p “link:#{link}”
while true
sleep(5)
end
end
end
end
while true
end
outputs:
0
“link:#Thread:0x1021f34”
1
“link:#Thread:0x163956”
2
“link:#Thread:0x10e434d”
3
“link:”
4
“link:”
some thread is nil.
in ruby, is not nil…
A Bug for JRUBY !!!
I don’t know if I’d characterize this as a bug, and definitely not
with a bug in Jruby (IMHO)… What you seem to have here is a classic
threading issue…
in the main thread you creating a thread. And the result of calling
new is assigned to link.
But passing a thread a block automatically starts the thread with that
block. So you don’t have control over who gets priority. Sometimes
it’s the new thread kicking off before the assignment to link has
happened and sometimes not.
if you sleep for 1 before you print time and link… you get different
results.
It sounds like you are trying to do something in a “deferred” manner.
Perhaps you should look into ways of creating the thread first… THEN
starting it?
On Thu, Jul 8, 2010 at 8:21 AM, Lei K. [email protected] wrote:
end
0
some thread is nil.
http://xircles.codehaus.org/manage_email
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Jay McGaffigan wrote:
I don’t know if I’d characterize this as a bug, and definitely not
with a bug in Jruby (IMHO)… What you seem to have here is a classic
threading issue…
in the main thread you creating a thread. And the result of calling
new is assigned to link.
But passing a thread a block automatically starts the thread with that
block. So you don’t have control over who gets priority. Sometimes
it’s the new thread kicking off before the assignment to link has
happened and sometimes not.
if you sleep for 1 before you print time and link… you get different
results.
It sounds like you are trying to do something in a “deferred” manner.
Perhaps you should look into ways of creating the thread first… THEN
starting it?
On Thu, Jul 8, 2010 at 8:21 AM, Lei K. [email protected] wrote:
�end
0
some thread is nil.
� �http://xircles.codehaus.org/manage_email
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Can you give an example?
I check the ruby-doc/core, but there is not a method to: first create a
thread, then run it.
thanks a lot!
I finallly solve like this:
Thread.new do
5.times do |time|
Thread.new do
link = Thread.current
p time
p “link:#{link}”
while true
sleep(5)
end
end
end
end
while true
end
thanks all
Jay McGaffigan wrote:
I don’t know if I’d characterize this as a bug, and definitely not
with a bug in Jruby (IMHO)… What you seem to have here is a classic
threading issue…
in the main thread you creating a thread. And the result of calling
new is assigned to link.
So the problem is that the interior thread actually executes its print
statements before link is assigned using the outside thread.
http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Thread#Thread_local_variables
might help.