File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes SingleThreadExecutor suggestions  needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "SingleThreadExecutor suggestions  needed" Watch "SingleThreadExecutor suggestions  needed" New topic
Author

SingleThreadExecutor suggestions needed

pravin gate
Greenhorn

Joined: Feb 20, 2012
Posts: 10
I am trying to create a simple Thread program using newSingleThread Executor.

So here is my sample code.





As we can see here I have just created a object of ExecutorService and I am trying to execute a task.Up to this it's clear.

But If I am getting any exceptions I just want to restart my task So in this scenario how can I do that?

Any suggestions will be helpful.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi,
You have to use Callable in place of Runnable in this case.

API quote:
"The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception"

Callable is ideal for your situation because there is a possibility of exception being thrown by the task. The signature of the 'call()' method has a throws clause. Your code could look like this:


Key points about this code:
1) Note that I do not have any try/catch block within my task. If I handle the exception myself (and do not rethrow it), the task execution will be considered as success.
2) Any exception thrown from the task is masked as ExecutionException. So, you have to catch this exception (and not the exception you expect - in your case, the IOException)
3) I have submitted the task just one more time. In real world situation, you might have to do this within a loop with a wait time. You have mentioned ChatLogin in your code, so, I guess this is socket stuff. So, in real-world scenario, if I have an network exception, I would wait for some time before submitting the task again. This would be application logic that you will have to handle.

One more point: you have called executorService.shutdown() in your code. Note that, once you call shutdown, the executor service will not process any more tasks. So, you have to be careful with that.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
pravin gate
Greenhorn

Joined: Feb 20, 2012
Posts: 10
I am trying your way, But this

is forcing me to either throws exception or handle it? Can You explain it?

Ranganathan Kaliyur Mannar wrote:Hi,
You have to use Callable in place of Runnable in this case.

API quote:
"The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception"

Callable is ideal for your situation because there is a possibility of exception being thrown by the task. The signature of the 'call()' method has a throws clause. Your code could look like this:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

I have already explained it earlier:
Ranganathan Kaliyur Mannar wrote:
The signature of the 'call()' method has a throws clause.

2) Any exception thrown from the task is masked as ExecutionException. So, you have to catch this exception (and not the exception you expect - in your case, the IOException)


Did you go through the Callable API?

How would you know that your task threw an exception unless it is rethrown by the callableResult.get() method? Earlier, you had a try/catch block within the task to handle this - but, that doesn't make it easy to rerun the task. Callable on the other hand does just that. It gives you a chance to handle the exception - in your case, you can use this to re-run the task.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

Sorry, but Callable seems like the bass-ackwards way to do this. If your Runnable needs to retry in case of error, it should just catch the exception and retry. Having the main thread wait on a Callable defeats the whole purpose of having a thread. I am assumung that the reason you want to put that bit of code in a background thread is because you want the main thread to either return or go do something else. If you are doing Callable.get on the main thread, your main thread will wait till your worker thread exits. What's the point in having a worker thread then?

What you need to do is this

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

hmm...ok, I see the point you are making.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SingleThreadExecutor suggestions needed
 
Similar Threads
Information Regarding SingleThreadExecutor
Callable and SWT - How to keep Thread in Background when startet by ButtonDown-Event
How to correctly use a fixed size thread pool?
Graphics 2d Issue
Java program for fibanocci using Java threads