本文介绍了如果从scheduleWithFixedDelay/scheduleAtFixedRate方法中检索到ScheduledFuture.get()方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下内容感到困惑.

I am confused with the following.

我知道,如果我使用ScheduledThreadPoolExecutor类中的schedule方法:

I know, if I use the schedule method from the ScheduledThreadPoolExecutor class:

ScheduledFuture<?> scheduledFuture =
scheduledThreadPoolExecutor.schedule(myClassRunnable, 5, TimeUnit.SECONDS);

我以后可以通过获取scheduledFuture.get(5, TimeUnit.SECONDS)scheduledFuture.get(),并且应为null,因为该任务仅执行了一次并且已完成.还有null,因为我使用的是Runnable schedule方法版本,而不是Callable schedule方法版本.符合API.

I am able to retrieve later the value throughscheduledFuture.get(5, TimeUnit.SECONDS) or scheduledFuture.get() and it should be null because the task has been executed just only once and it is completed.And null because I am working with the Runnable schedule method version and not with the Callable schedule method version. It according with the API.

直到这里我还好.

我的问题:

如果从scheduleWithFixedDelay(甚至从scheduleAtFixedRate)方法检索到ScheduledFuture的目的是什么:

What is the purpose of ScheduledFuture if is retrieved from the scheduleWithFixedDelay (even from scheduleAtFixedRate) method:

ScheduledFuture<?> scheduledFuture=
scheduledThreadPoolExecutor.scheduleWithFixedDelay(myClassRunnable, 1, 5, TimeUnit.SECONDS);

是的,我知道这两个 fixed 方法都多次执行同一任务,直到调用ScheduledThreadPoolExecutor's shutdown方法(它必须停止所有计划的任务).

Yes, I know both fixed methods execute the same task many times until the ScheduledThreadPoolExecutor's shutdown method is called (it must stop all the tasks scheduled).

我通过Google进行了一项研究,使用从scheduleWithFixedDelay返回的ScheduledFuture查找了一些示例,但我只找到了一个使用 cancel 方法来取消特定任务的示例.但是没有一个与get()一起使用.

I did a research through Google looking for some examples using ScheduledFuture returned from scheduleWithFixedDelay, I only found one using the cancel method, to cancel a specific task. But none working with get().

我不知道我是否写错了,但是如果我们正在使用scheduleWithFixedDelay,那么get()方法似乎就没用了,因为如果我稍后使用的话:

I don't know if I am wrong, but seems useless the get() method if we are working with scheduleWithFixedDelay, because if I use later:

  • scheduledFuture.get()-它保持等待状态,Runnable对象保持工作多次(运行,完成,延迟,运行等...)
  • scheduledFuture.get(32, TimeUnit.SECONDS)-始终给出TimeoutException
  • scheduledFuture.get() - it remains awaiting and the Runnable object remains working many times (run,complete,delay,run,etc... )
  • scheduledFuture.get(32, TimeUnit.SECONDS) - always gives a TimeoutException

我认为我应该能够检索null值,因为我可以使用scheduleWithFixedDelay方法中的period参数/参数.即:运行Runnable object,等待直到完成,然后使用scheduledFuture.get()获取确认已完成的null值,等待一段时间后再根据Runnable对象再次运行Runnable对象. period值等...

I thought I should be able to retrieve the null value since I can use the period argument/parameter from the scheduleWithFixedDelay method. I.e: Run the Runnable object, wait until it completes and use the scheduledFuture.get() to get the null value that confirms it has been completed, await the period of the delay time to run again the Runnable object according with the period value etc....

完全欢迎澄清和示例.

推荐答案

ScheduledFuture可用于获取下一次执行任务之前的时间:

ScheduledFuture can be used to get time left before next task execution:

    ScheduledFuture<?> f = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
        public void run() {
            System.out.println("run");
        }
    }, 0, 10000, TimeUnit.MILLISECONDS);
    Thread.sleep(1000);
    System.out.println("Time left before next run " + f.getDelay(TimeUnit.MILLISECONDS));

打印

run
Time left before next run 8999

这篇关于如果从scheduleWithFixedDelay/scheduleAtFixedRate方法中检索到ScheduledFuture.get()方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:22
查看更多