0

我有一个任务,我称它为 MyTask,它基本上是一个 Callable 类。说:

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


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

我在 Worker 类中有一个 singleThreadpoolExecutorService,它应该在调用我的 worker 类时初始化。

并且服务应该继续运行,直到我向对象提交一个 MyTask ......在执行时返回我可调用的结果。

现在,我的问题是,

服务应该继续运行,并继续以未来对象的形式接受输入。并执行它并交付结果。我该怎么做,这样服务就不会在两者之间停止。

当从外部标志(比如 isShutDown)中调用该服务时,应该停止该服务。

你能帮忙吗?

谢谢你。

4

1 回答 1

1

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. i.e. 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.

于 2013-06-08T10:29:36.833 回答