Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Don't wait for Exception in threads

Reply
Thread Tools

Don't wait for Exception in threads

 
 
Emil Sandin
Guest
Posts: n/a
 
      05-04-2008
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/.

 
Reply With Quote
 
 
 
 
James Gray
Guest
Posts: n/a
 
      05-04-2008
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

 
Reply With Quote
 
 
 
 
akl
Guest
Posts: n/a
 
      05-04-2008
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
 
Reply With Quote
 
Emil Sandin
Guest
Posts: n/a
 
      05-04-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
How to make "fork/wait" to WAIT longer? Huey C Programming 1 03-01-2004 02:01 PM
java Bug? -> Java threads: Wait X Notify X [Thread AGAINST Runnable] Renato Battaglia Java 3 10-24-2003 04:54 PM
How to wait for user console input OR (!!!) changing of a variable contents (threads used) Tobias Merler Java 0 08-25-2003 07:04 PM



Advertisments