Ruby Thread usage

Thread usage

#!/usr/local/bin/ruby

arr = Array.new

for j in 0..100000
   arr[j] = rand(1000) # 1000 is max int
end

# Threads A and B both loop continually, printing one message,
# sleeping for half a second, and then passing control to
# another thread
# threads start automactically

threadA = Thread.new do
   i = 0
   loop do
      puts "Thread A: #{i += 1}"
      puts "Thread A: #{arr[i]}"
      i += 1
      sleep 1.5
      Thread.pass
      if i >= 10 
         Thread.stop
      end
   end
end

threadB = Thread.new do
   i = 10000
   loop do
      puts "Thread B: #{i += 1}"
      puts "Thread B: #{arr[i]}"
      sleep 0.5
      Thread.pass
      if i >= 10010 
         Thread.stop
      end
   end
end

# Thread C also loops continually, printing a message. However,
# it stops after each message, waiting to be wokenup again by
# another thread (in this case, the main program)
threadC = Thread.new do
   i = 0
   loop do
      puts "Thread C: #{i += 1}"
      Thread.stop
   end
end

# The main program loops continually, doing nothing other than
# sleeping for half a second and then letting Thread C have a go

# main routine
loop do
   sleep 0.5
   threadC.run
end