简体   繁体   English

如何使executorService保持运行?

[英]How to keep an executorService running?

I have a task, I am calling it MyTask which is basically a Callable class. 我有一个任务,我称之为MyTask,它基本上是一个Callable类。 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. 我在Worker类中有一个singleThreadpoolExecutorService,应在初始化我的worker类时在初始化时启动。

And the service should keep on running , till i submit a MyTask to the object.... which, on execution returns me the result of callable. 并且该服务应继续运行,直到我向该对象提交MyTask为止。

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. 当从外部标志(例如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. 如果您认为它正在关闭,则说明您做错了事,但是不清楚您要问什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在executorservice中继续监听任务 - How to keep listening for tasks in executorservice Java ExecutorService-为什么该程序保持运行? - Java ExecutorService - why does this program keep running? Java - 如何在外部ExecutorService上调用shutdown时,使ExecutorService在另一个ExecutorService中运行而不关闭? - Java - How to make an ExecutorService running inside another ExecutorService not to shutdown when shutdown is invoked on the outer ExecutorService? 如何检查线程是否在ExecutorService线程池中运行 - How to check if a thread is running in the ExecutorService Thread pool 如何检查 ExecutorService 上运行的所有任务是否已完成 - How to check if all tasks running on ExecutorService are completed 具有正在运行的任务数的ExecutorService - ExecutorService with number of running tasks 如何知道java中的ExecutorService中是否有正在运行的任务? - How to know whether there're running tasks in ExecutorService in java? 如何等待ExecutorService中正在运行的线程之一完成以分配其他任务 - How to wait for one of the running threads in ExecutorService to finish to assign another task Java ExecutorService运行线程两次 - Java ExecutorService running threads twice 发送到ExecutorService的作业的运行时间 - Running time of a job sent to a ExecutorService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM