0

I want to throw an exception from a runnable thread but its not possible to throw it from thread so can we pass the status(any exception ) of chlild thread to parent thread?.

I read about thread.join() but in this case parent thread waits untill child thread finishes its excecution.

In my case my parent thread start threads one by one after some period of time ,but when any thread throws exception ,it should notify the paent about the failure so that parent thread does not start other threads.

is there any way to implement it ? can anyone help me do solve this.

pbhle
  • 2,856
  • 13
  • 33
  • 40

4 Answers4

4

To elaborate on @zeller's answer, you could do something like the following construct:

//Use a Callable instead of Runnable to be able to throw your exception
Callable<Void> c = new Callable<Void> () {
    public Void call() throws YourException {
        //run your task here which can throw YourException
        return null;
    }
}

//Use an ExecutorService to manage your threads and monitor the futures
ExecutorService executor = Executors.newCachedThreadPool();
List<Future> futures = new ArrayList<Future> ();

//Submit your tasks (equivalent to new Thread(c).start();)
for (int i = 0; i < 5; i++) {
    futures.add(executor.submit(c));
}

//Monitor the future to check if your tasks threw exceptions
for (Future f : futures) {
    try {
        f.get();
    } catch (ExecutionException e) {
        //encountered an exception in your task => stop submitting tasks
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • But what if i don't know how many threads my application will call ,then my futures list will not be fixed.?? Then how to monitor them?? – pbhle Aug 30 '12 at 10:35
  • If you store the futures returned by executor.submit in the list every time you submit a new task, you don't need to know how many threads are running, you only need to keep track of those futures. – assylias Aug 30 '12 at 10:40
  • ok.. Because i am calling this callable method from 2-3 classes and using only one futures list where i am adding which is returned by executor.submit .. – pbhle Aug 30 '12 at 10:59
  • Yes that's the idea. Alternatively, you can use a CompletionService, which will return the tasks as they get completed. See for example: http://stackoverflow.com/questions/11578326/optimizing-parallel-processing-of-many-files/11578512#11578512 – assylias Aug 30 '12 at 11:04
2

You can use Callable<Void> instead of Runnable and also an ExecutorService instead of a custom thread pool. Callable-s call throws an exception.
Using an ExecutorService also provides the possibility to manage your running tasks tracking the Future-s returned by submit. This way you'll be aware of exceptions, task completion and so.

zeller
  • 4,904
  • 2
  • 22
  • 40
0

Instead of implementing Runnable interface implement Callable interface, to return value to parent thread.

I want to throw an exception from a runnable thread but its not possible to throw it from thread so can we pass the status(any exception ) of chlild thread to parent thread?.

--> @assylias says : don't pass the exception via a return value, just throw it. You can then catch it from the parent thread, typically with a future.get(); call which will throw an ExecutionException.

Also, Callable.call() throws Exception so you can directly throw it.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • I don't agree with your last comment: don't pass the exception via a return value, just throw it. You can then catch it from the parent thread, typically with a `future.get();` call which will throw an `ExecutionException`. – assylias Aug 30 '12 at 09:21
  • @assylias : I know in case of Runnable, we have to handle it in thread. I just know the way: using Callable interface. In practice I never used, Callable. I will update answer shortly. – Nandkumar Tekale Aug 30 '12 at 09:25
  • @assylias : BTW, `call() throws Exception`, do we need to get it using `future.get();` – Nandkumar Tekale Aug 30 '12 at 09:32
  • I have added an answer with an example – assylias Aug 30 '12 at 09:33
0

Use a concurrent collection to communicate between the parent and the child threads. In your run method, do a try/catch block to receive all exceptions, and if one happens, append it to the collection used to communicate with the parent. The parent should check the collection to see if any errors occurred.

vainolo
  • 6,907
  • 4
  • 24
  • 47