Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确使用ScheduledExecutorService?_Java_Scheduledexecutorservice_Executors - Fatal编程技术网

Java 如何正确使用ScheduledExecutorService?

Java 如何正确使用ScheduledExecutorService?,java,scheduledexecutorservice,executors,Java,Scheduledexecutorservice,Executors,所以这是我第一次使用ScheduledFuture,我承认我在这里可能有点不知所措。我似乎无法使下面的示例正常工作。我们的目标只是在继续下一组动作并无限期重复之前,采取两组动作,每个动作都有自己的超时时间 static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); ScheduledFuture<?> warm = executor.scheduleWithFixedDelay(()

所以这是我第一次使用ScheduledFuture,我承认我在这里可能有点不知所措。我似乎无法使下面的示例正常工作。我们的目标只是在继续下一组动作并无限期重复之前,采取两组动作,每个动作都有自己的超时时间

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);}