Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what cases does Future.get() throw ExecutionException or InterruptedException

My code snippet:

ExecutorService executor = Executors.newSingleThreadExecutor(); try {     Task t = new Task(response,inputToPass,pTypes,unit.getInstance(),methodName,unit.getUnitKey());     Future<SCCallOutResponse> fut = executor.submit(t);     response = fut.get(unit.getTimeOut(),TimeUnit.MILLISECONDS); } catch (TimeoutException e) {     // if the task is still running, a TimeOutException will occur while fut.get()     cat.error("Unit " + unit.getUnitKey() + " Timed Out");     response.setVote(SCCallOutConsts.TIMEOUT); } catch (InterruptedException e) {     cat.error(e); } catch (ExecutionException e) {     cat.error(e); } finally {     executor.shutdown(); } 

How should i handle the InterruptedException and ExecutionException in the code?

And in what cases, are these exceptions thrown?

like image 961
java_geek Avatar asked Apr 19 '10 06:04

java_geek


People also ask

Which exception is thrown when we invoke future get ()?

get() throw ExecutionException or InterruptedException.

What throws an InterruptedException?

3. What Is an InterruptedException? An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt() method on our thread.

Should I catch InterruptedException?

If the blocking network call throws an InterruptedException your method can not finish computation in a normal way. You let the InterruptedException propagate. If no, then you should not declare your method with throws InterruptedException and you should (must!) catch the exception.

What is the use of throws InterruptedException in Java?

Class InterruptedException. Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception.


1 Answers

ExecutionException and InterruptedException are two very different things.

ExecutionException wraps whatever exception the thread being executed threw, so if your thread was, for instance, doing some kind of IO that caused an IOException to get thrown, that would get wrapped in an ExecutionException and rethrown.

An InterruptedException is not a sign of anything having gone wrong. It is there to give you a way to let your threads know when it's time to stop so that they can finish up their current work and exit gracefully. Say I want my application to stop running, but I don't want my threads to drop what they're doing in the middle of something (which is what would happen if I made them daemon threads). So when the application is being shutdown, my code calls the interrupt method on these threads, which sets the interrupt flag on them, and the next time those threads are waiting or sleeping they check the interrupt flag and throw an InterruptedException, which I can use to bail out of whatever infinite-loop processing/sleeping logic the threads are engaged in. (And if the thread doesn't wait or sleep, it can just check the interrupt flag periodically.) So it is an instance of an exception being used to change the logical flow. The only reason you would log it at all is in an example program to show you what's happening, or if you're debugging a problem where interrupt logic is not working correctly.

like image 85
Nathan Hughes Avatar answered Sep 23 '22 21:09

Nathan Hughes