java ScheduledExecutorService 一个线程多任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13927816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ScheduledExecutorService one thread many tasks
提问by Nikolay Kuznetsov
I am new to ExecutorService
and wonder why following code prints correctly "10 15", even though I have created only one thread to process the timeouts? Why can I call schedule many times without previous tasks being cancelled on a single thread executor?
我是新手ExecutorService
,想知道为什么即使我只创建了一个线程来处理超时,以下代码仍会正确打印“10 15”?为什么我可以多次调用 schedule 而不会在单个线程执行器上取消以前的任务?
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestExecutorService implements Runnable {
public static ScheduledExecutorService SERVICE = Executors.newSingleThreadScheduledExecutor();
private int delay;
public TestExecutorService(int delay) {
this.delay = delay;
}
public void run () {
System.out.println(delay);
}
public static void main (String[] args) {
SERVICE.schedule(new TestExecutorService(10), 10, TimeUnit.SECONDS);
SERVICE.schedule(new TestExecutorService(15), 15, TimeUnit.SECONDS);
SERVICE.shutdown();
}
}
回答by Brian
From the Javadocs:
从Javadocs:
Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.
任务保证按顺序执行,并且在任何给定时间不会有超过一个任务处于活动状态。
The difference between "processing timeouts" and "task execution" is the key to the answer. You assume that "single-threaded" means "processing only one timeout at a time", but it really means "executing only one task at a time". All the timeouts are processed simultaneously, but if one timeout is reached before a task stops executing, it will have to wait for the other one to finish before it can execute.
“处理超时”和“任务执行”的区别是答案的关键。您假设“单线程”意味着“一次仅处理一次超时”,但它实际上意味着“一次仅执行一项任务”。所有超时都是同时处理的,但是如果在任务停止执行之前达到一个超时,则必须等待另一个超时才能执行。
回答by Perception
Reading the Javadocwould be very helpful in this case. It does explain that though the executor will be created with a single thread, it will be operating with an unboundedqueue. This means that you can submit multiple tasks to the executor and they will be queued up to run one after the other, up to the maximum bounds of the queue (which in this case is infinity) or until the JVM runs out of resources.
在这种情况下,阅读Javadoc将非常有帮助。它确实解释了尽管 executor 将使用单个线程创建,但它将使用无界队列运行。这意味着您可以将多个任务提交给执行器,它们将一个接一个地排队运行,直到队列的最大边界(在本例中为无穷大)或直到 JVM 耗尽资源。
Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.
创建一个 Executor,它使用单个工作线程在无界队列中运行。(但是请注意,如果这个单线程在关闭之前的执行过程中由于失败而终止,如果需要执行后续任务,一个新线程将取而代之。)保证任务按顺序执行,并且不会超过一个任务处于活动状态在任何给定的时间。与其他等效的 newFixedThreadPool(1) 不同,返回的执行程序保证不可重新配置以使用其他线程。
In your example your two tasks get queued up, and run sequentially one after the other, which is why you get the (expected) output.
在您的示例中,您的两个任务排队并依次运行,这就是您获得(预期)输出的原因。