We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

chrisaiv on 10/22/07


Tagged

ruby Threading


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

mehlam2
Netzach


Simple Example of Threading in Ruby


Published in: Ruby 


You can run functions in parallel without any problems as long as you have enough memory in your computer and none of your functions are dependant on one another. THink about how quickly you can complete your cron jobs now!

  1. def func1
  2. i = 0
  3. while i <= 5
  4. puts "func1 at: #{Time.now}"
  5. sleep(2)
  6. i = i + 1
  7. end
  8. end
  9. def func2
  10. i = 0
  11. while i <= 5
  12. puts "func2 at: #{Time.now}"
  13. sleep(1)
  14. i = i + 1
  15. end
  16. end
  17.  
  18. puts "Start at: #{Time.now}"
  19. t1 = Thread.new{func1()}
  20. t2 = Thread.new{func2()}
  21. t1.join
  22. t2.join
  23. puts "End at: #{Time.now}"

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: DIGJAM on March 18, 2008

HI

You need to login to post a comment.