Java 如何正确使用ScheduledExecutorService?
所以这是我第一次使用ScheduledFuture,我承认我在这里可能有点不知所措。我似乎无法使下面的示例正常工作。我们的目标只是在继续下一组动作并无限期重复之前,采取两组动作,每个动作都有自己的超时时间Java 如何正确使用ScheduledExecutorService?,java,scheduledexecutorservice,executors,Java,Scheduledexecutorservice,Executors,所以这是我第一次使用ScheduledFuture,我承认我在这里可能有点不知所措。我似乎无法使下面的示例正常工作。我们的目标只是在继续下一组动作并无限期重复之前,采取两组动作,每个动作都有自己的超时时间 static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); ScheduledFuture<?> warm = executor.scheduleWithFixedDelay(()
static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
ScheduledFuture<?> warm = executor.scheduleWithFixedDelay(() -> {
System.out.println("warmbeans");
//do more stuff here
}, 0, 1000, TimeUnit.MILLISECONDS);
ScheduledFuture<?> cool = executor.scheduleWithFixedDelay(() -> {
System.out.println("coolbeans");
//do more stuff here
}, 0, 500, TimeUnit.MILLISECONDS);
while(true) {
try {warm.get(1000, TimeUnit.MILLISECONDS);}
catch (InterruptedException | ExecutionException | TimeoutException e) {Listen.cancel(true);}
try {cool.get(500, TimeUnit.MILLISECONDS);}
catch (InterruptedException | ExecutionException | TimeoutException e) {Listen.cancel(true);}
上面提到的NetworkIO.java:29行只是:
try {warm.get(1000, TimeUnit.MILLISECONDS);}
try {warm.get(1000, TimeUnit.MILLISECONDS);}