简体   繁体   中英

How to keep an executorService running?

I have a task, I am calling it MyTask which is basically a Callable class. Say:

public class MyTask implements Callable
{
// my custom code which returns from call()
}


public class Worker
{
 ExecutorService service = Executors.newSingleThreadExecutor();
}

I have a singleThreadpoolExecutorService inside the Worker class, which should start off, on initialization when my worker class is called.

And the service should keep on running , till i submit a MyTask to the object.... which, on execution returns me the result of callable.

Now, my issue is,

The service should keep on runnning, and keep on accepting input in the form of future objects. and execute it and deliver a result. How do I do it , so that the service is not stopped in between.

the service should be stopped when called off from an external flag- say isShutDown.

Can you please help?

Thank you.

The executor service will keep running until you shutdown() it. It will even keep running if there is no other non-daemon thread and there is nothing for it to do. ie keep your program running even though it might not really be needed.

If you believe it is shutting down, you are doing something wrong, but it is not clear what for your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM