Don't wait for Exception in threads

Discussion in 'Ruby' started by Emil Sandin, May 4, 2008.

  1. Emil Sandin

    Emil Sandin Guest

    I have two threads running in parallell, like this:

    ####################

    t1=Thread.new{
    50.times{
    p "thread one"
    sleep 0.1
    }
    }

    t2=Thread.new{
    p "thread two"
    raise "error in thread two"
    }

    t1.join
    t2.join

    #################
    The output is:
    "thread one"
    "thread two"
    "thread one"
    ... 48 times more "thread one"
    "thread one"
    testscript.rb:15: error in thread two (RuntimeError)
    from testscript.rb:19:in `join'
    from testscript.rb:19

    The program waits for the first thread to complete before telling me
    that the second one raised an exception.
    I don't want to wait until the first thread finishes to see the
    exception, I want to see it right away.
    How can I do that?

    Regards
    Emil
    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, May 4, 2008
    #1
    1. Advertising

  2. Emil Sandin

    James Gray Guest

    On May 4, 2008, at 1:48 PM, Emil Sandin wrote:

    > I have two threads running in parallell, like this:
    >
    > ####################
    >
    > t1=Thread.new{
    > 50.times{
    > p "thread one"
    > sleep 0.1
    > }
    > }
    >
    > t2=Thread.new{
    > p "thread two"
    > raise "error in thread two"
    > }
    >
    > t1.join
    > t2.join
    >
    > #################
    > The output is:
    > "thread one"
    > "thread two"
    > "thread one"
    > ... 48 times more "thread one"
    > "thread one"
    > testscript.rb:15: error in thread two (RuntimeError)
    > from testscript.rb:19:in `join'
    > from testscript.rb:19
    >
    > The program waits for the first thread to complete before telling me
    > that the second one raised an exception.
    > I don't want to wait until the first thread finishes to see the
    > exception, I want to see it right away.
    > How can I do that?


    Add this line early in your program:

    Thread.abort_on_exception = true

    Hope that helps.

    James Edward Gray II
     
    James Gray, May 4, 2008
    #2
    1. Advertising

  3. Emil Sandin

    akl Guest

    Hi Emil,

    Emil Sandin schrieb:
    > I have two threads running in parallell, like this:
    >
    > ####################
    >
    > t1=Thread.new{
    > 50.times{
    > p "thread one"
    > sleep 0.1
    > }
    > }
    >
    > t2=Thread.new{
    > p "thread two"
    > raise "error in thread two"
    > }
    >
    > t1.join
    > t2.join
    >
    > #################
    > The output is:
    > "thread one"
    > "thread two"
    > "thread one"
    > ... 48 times more "thread one"
    > "thread one"
    > testscript.rb:15: error in thread two (RuntimeError)
    > from testscript.rb:19:in `join'
    > from testscript.rb:19
    >
    > The program waits for the first thread to complete before telling me
    > that the second one raised an exception.
    > I don't want to wait until the first thread finishes to see the
    > exception, I want to see it right away.
    > How can I do that?


    You can do something like:

    Thread.abort_on_exception = true
    t = []
    t << Thread.new {
    50.times{
    p "thread one"
    sleep 0.1
    }
    }
    t << Thread.new {
    p "thread two"
    raise "error in thread two"
    }
    t.each {|t| t.join }

    I dont have tested the example, but this way you should see the Exception in the
    Moment it occurs

    Andi
     
    akl, May 4, 2008
    #3
  4. Emil Sandin

    Emil Sandin Guest

    James Gray wrote:
    > On May 4, 2008, at 1:48 PM, Emil Sandin wrote:
    >
    >>
    >> "thread one"
    >> I don't want to wait until the first thread finishes to see the
    >> exception, I want to see it right away.
    >> How can I do that?

    >
    > Add this line early in your program:
    >
    > Thread.abort_on_exception = true
    >
    > Hope that helps.
    >
    > James Edward Gray II


    Thank you, that's exactly what I am looking for.

    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, May 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobias Merler
    Replies:
    0
    Views:
    462
    Tobias Merler
    Aug 25, 2003
  2. Renato Battaglia
    Replies:
    3
    Views:
    2,821
    Jon Skeet
    Oct 24, 2003
  3. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,981
    -berlin.de
    Mar 1, 2004
  4. Jason Cavett

    Don't Understand wait() and notify()

    Jason Cavett, Mar 28, 2007, in forum: Java
    Replies:
    27
    Views:
    705
    Martin Gerner
    Apr 27, 2007
  5. Richard Morey
    Replies:
    3
    Views:
    562
    Richard Morey
    Jan 22, 2004
Loading...

Share This Page