2

I've got an application which regularly submits tasks to be executed in a dedicated thread. Those tasks are FutureTask<V> and the thread is no more than an infinite loop which executes the jobs as they get into a queue, going to sleep if empty.

Sometimes I need to wait for the result of a computation, so I call FutureTask's get() method, which will also throw an exception if something bad happened. But there are cases where I don't care what the result was, if any, but I need to know (log, printStackTrace() or whatever...) if there was some kind of failure during the task execution.

Is there any way I could achieve this without having to wait on a get() that I don't need?

Thanks in advance!

UPDATE: Calling get() on the runner thread after executing the task seemed to be the only way to catch the exception if no one else would wait for the result.

Another solution would be extending FutureTask class to provide a method which, via java reflection, exposes sync.exception private field.

user683887
  • 1,260
  • 1
  • 10
  • 20

5 Answers5

0

Check Thread.UncaughtExceptionHandler

kofemann
  • 4,217
  • 1
  • 34
  • 39
  • could you add more information about this? I tried to set an UncaughtExceptionHandler on the runner thread, but nothing changed, as I think those exceptions are not ungaught, but instead managed by FutureTask and not exposed until get() method is called. Thanks – user683887 May 06 '12 at 14:33
0

If I understand your problem correctly, there is a simple solution. Modify your dedicated task-runner thread to call Future.get() after running each task, and catch and log the ExecutionException (if any).

Note that calling get in the task-runner thread after running the task is guaranteed not to block.

(Uncaught exception handlers won't help because any exception and error thrown by the run() method of the future's wrapped Runnable will be caught byFutureTask.run()`.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I can't find this getException() method on Future or FutureTask. Am I missing something? Thanks for your answer – user683887 May 06 '12 at 14:57
0

I think you should use 'setException' to report the failure from your task. It will make get() report an ExecutionException. See this as well:

How to catch exceptions in FutureTask

Community
  • 1
  • 1
Jouni Aro
  • 2,099
  • 14
  • 30
  • calling get() is what I want to avoid ;-) I sometimes submit long running tasks to save information on a database... On this cases I don't want to be blocked waiting on a get() but I would like to see possible exceptions logged somewhere. Thanks – user683887 May 06 '12 at 15:03
  • why don't you then just catch exceptions in your thread and log them? – Jouni Aro May 06 '12 at 15:08
  • because FutureTask's run method just doesn't throw exceptions, so even if I put it inside a try-catch block nothing would be logged. – user683887 May 06 '12 at 15:11
  • But you can catch them in your Runnable.run() that you provide for the FutureTask, can't you? – Jouni Aro May 06 '12 at 15:13
  • Well, yes, I could catch them inside my Callables, but I was hoping there was a simpler way to handle this, without requiring me to modify all my predefined job classes and also to be able to log them even if I forget to add the try-catch block to a job. – user683887 May 06 '12 at 15:21
  • 1
    OK, I see. If you check the link I provided, the user there tries to log in the overridden setException. Only according to the last response, that only works since Java 7, due to a Java bug. – Jouni Aro May 06 '12 at 15:25
  • Yes, I also tried to override setException() to see if exceptions could be logged there but no luck. Unfortunately, I can't move to java 7 yet. – user683887 May 06 '12 at 15:32
0

A CompletionService might be a possibility here. Using one of those will allow you to call get() on the Futures once they are definitely finished. Alternatively, the Guava libraries have ways to add a listener to a Future or FutureTask to execute when the task is complete (the ListenableFuture and its related objects).

Paul Blessing
  • 3,815
  • 2
  • 24
  • 25
0

You can also check an example I wrote for Futuretask

Chrys
  • 636
  • 1
  • 7
  • 18