sleeping Threads

B

Boris Glawe

Hi,

I'd like to start thousands of threads, each sleeping a random time and printing
a string to the terminal.

How do you stop a thread for a certain time without stopping the whole process?

1000.times{ |i|
Thread.new{
sleep(rand(10)) # this method does not only stop the thread but the whole
thread!!
puts ("hello world says #{i.to_s}")
}
}

thanks and greets

Boris
 
B

Boris Glawe

This is of course wrong:
1000.times{ |i|
Thread.new{
sleep(rand(10)) # this method does not only stop the thread but
the whole thread!!
puts ("hello world says #{i.to_s}")
}
}


it must be



1000.times{ |i|
Thread.new(i){ |x|
sleep(rand(10))
puts ("hello world says #{x.to_s}")
}
}


greets Boris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sleeping threads don't wake up? 5
Threads 1
background threads 5
Threads After Fork 3
question on threads 3
Thread problem 4
Eventmachine questions? 1
Using threads to show progress 21

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top