• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SingleThreadExecutor suggestions needed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pravin gate
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm...ok, I see the point you are making.
 
reply
    Bookmark Topic Watch Topic
  • New Topic