Ruby (and generally code) beginner question:
I'm reading about threads in the pickaxe book.
To understand what's going on I was trying to run the following code:
===============
threads=[]
3.times do |i|
threads[i]=Thread.new do
puts i
end
end
===============
It ran ok, but nothing appeared on the console. I was expecting each of
the three threads to print 'their' position in the thread array.
The point of this exercise was to look at threads while they are working
concurrently, before calling the thread.join method.
I guess I'm misunderstanding the concept because this doesn't work.
my questions are:
1. what's wrong with the above code
2. can I write a program that will create M threads each of which
loops from 1 to N and all of them print concurrently to the console
something like "Thread #{m} is now on number #{n}"
Thanks.
on 2012-11-18 13:46
on 2012-11-18 13:54
On Sunday 18 November 2012 Assaf Shomer wrote > end > 1. what's wrong with the above code > 2. can I write a program that will create M threads each of which > loops from 1 to N and all of them print concurrently to the console > something like "Thread #{m} is now on number #{n}" > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. I'm not an expert using threads, but it seems that the program terminates before the threads have a chance to do anything. Try putting a sleep 1 after the cycle and you'll see what you expect. Stefano
on 2012-11-18 15:47
On Sun, Nov 18, 2012 at 1:46 PM, Assaf Shomer <lists@ruby-forum.com> wrote: > end > =============== > > It ran ok, but nothing appeared on the console. I was expecting each of > the three threads to print 'their' position in the thread array. > The point of this exercise was to look at threads while they are working > concurrently, before calling the thread.join method. > > I guess I'm misunderstanding the concept because this doesn't work. As Stefano already answered, the main thread is finishing before the child threads do anything. That's why the method join exists, so that the main thread can wait for the other threads. threads = (1..3).map do |i| Thread.new(i) do |j| puts j end end threads.each {|t| t.join } > my questions are: > 1. what's wrong with the above code > 2. can I write a program that will create M threads each of which > loops from 1 to N and all of them print concurrently to the console > something like "Thread #{m} is now on number #{n}" M = 3 N = 10 threads = (1..M).map do |i| Thread.new(i) do |thread_id| N.times do |n| puts "Thread #{thread_id}, #{n}" sleep(rand(2)) end end end threads.each {|t| t.join } The sleep makes each thread wait a random time each iteration, so you see how they interleave. Jesus.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.