Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do Exeption Handling in Multi Threaded Eniv

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to do exeption handling in Multi Threaded Eniv.
Using normal try/catch or join() method is not providing me good results.

Any pointers how to achieve excetion handling in an Multi Threaded Eniv ?

I read about "Multi threaded Exception Handling" article [http://www.adtmag.com/java/articleold.aspx?id=74#] but i am not able to understand the concept they have mentioned.

if any body can share some code samples , i will thankful to them.

Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, try/catch and join() do work in a multi-threaded environment. In what way are you not getting good results? What are you trying to achieve, and what is happening instead?
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yim

Thanks for replying ....

Using Try/Catch - problem is thread make a new stack ....so ... since main thread and second thread works in separate stack - it does not work well

Using join() method leads to the condition that the first thread should wait for second thread to finish - it almost leads to a single threaded application

So, I am trying to implement a exception handling in multi threaded eniv using Java 1.1 Event Model where "Event" will be an exception thrown by a thread. Now , when exception is thrown , i want other related threads to get a notification that thread had an exception. So , basically kind of "Observer" Pattern.I am not able to achieve that.

Please help me out in this.

Thanks
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I am trying to implement a exception handling in multi threaded eniv using Java 1.1 Event Model where "Event" will be an exception thrown by a thread. Now , when exception is thrown , i want other related threads to get a notification that thread had an exception. So , basically kind of "Observer" Pattern.I am not able to achieve that.


There is no magic here -- exceptions are only throuwn "upwards". If you want exceptions to be thrown across threads, then you need to implement some sort of mechanism/communication between your threads, to determine that an exception is thrown.

You need to catch it, save it, alert the other threads, rethrow it. With the other threads, you need to get the alert, get it from the saved location, and throw the exception.

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see what you're saying. If you're using JDK 5 or 6, you might look at java.util.concurrent, specifically Future, ExecutorService and ExecutorCompletionService. Future's get() method will rethrow the original exception when you get the result (from another thread). With ExecutorCompletionService's take() or poll() you can get (or wait for) the next completed task, so you needn't lose any concurrency by waiting for one task in particular.

Alternately if you really prefer the observer pattern, you can do something like this:

The idea is to wrap() any Runnable before you run it. You could also build this into an ExecutorService decorator, so that whenever you submit(), invokeAll() or invokeAny(), each Runnable or Callable is wrapped before being run. Though if you're using java.util.concurrent at all, you don't really need the observer pattern - just use the strategy from my first paragraph above.
[ March 01, 2008: Message edited by: Jim Yingst ]
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry and Jim

Thanks for replying...
i will try to apply above mentioned approach.

Thanks
Abhay Agarwal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!