我是新手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();
}
}