mutlithreading in ruby

thread1 = do
  1.upto(10) {|x| sum = sum + x}
  puts("The sum of the first 10 integers is #{sum}")

thread2 = do
  1.upto(10) {|x| product = product * x}
  puts("The product of the first 10 integers is #{product}")


While there is great power in multithreaded code, there is also a lot of danger. A
great way to introduce hard-to-find bugs into your programs is to allow two or more
threads to modify the same data structure at the same time. A good way to avoid this
and other race conditions and make your code thread safe is to use the Monitor class:

@monitor =
  @monitor.synchronize do
  # Only one thread at a time here...

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>