Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ScheduledFuture.get() method if is retrieved from the scheduleWithFixedDelay/scheduleAtFixedRate method

Tags:

I am confused with the following.

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

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

I am able to retrieve later the value through scheduledFuture.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.

Until here I am fine.

My question:

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

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).

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().

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() - 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

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....

Clarifications and examples are totally welcome.

like image 892
Manuel Jordan Avatar asked Jul 31 '14 00:07

Manuel Jordan


People also ask

What is Scheduledfuture in Java?

A delayed result-bearing action that can be cancelled. Usually a scheduled future is the result of scheduling a task with a ScheduledExecutorService .

What is ScheduledExecutorService in Java?

public interface ScheduledExecutorService extends ExecutorService. An ExecutorService that can schedule commands to run after a given delay, or to execute periodically. The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution.

Which of the following is the correct syntax of executor which is used to run a certain task periodically or at regular intervals?

Using ScheduledExecutorService The scheduleAtFixedRate() and scheduleWithFixedDelay() methods create and execute tasks that run periodically until cancelled.


1 Answers

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

prints

run
Time left before next run 8999
like image 55
Evgeniy Dorofeev Avatar answered Sep 28 '22 17:09

Evgeniy Dorofeev