From: Stefano Crocco Date: 2012-11-18T21:53:40+09:00 Subject: Re: A basic question about Threads On Sunday 18 November 2012 Assaf Shomer wrote > 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. > > -- > 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