Scheduling One-Shot Tasks

The overloaded schedule() method of the ScheduledExecutorService interface allows either a Runnable or a Callable<V> task to be scheduled for execution only once after a specified delay—that is, the schedule() methods execute scheduled one-shot tasks. The overloaded schedule() method of the ScheduledExecutorService interface is the analog of the overloaded submit() method of the ExecutorService interface for executing Runnable and Callable<V> tasks. These methods do not block—that is, they do not wait for the task to complete execution.

The following methods are defined in the ScheduledExecutorService interface that extends the ExecutorService interface:

Click here to view code image

ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit)

Schedules a one-shot task that becomes enabled after the given delay. The method returns a ScheduledFuture<?> object representing pending completion of the task and whose get() method will return null upon completion.

Click here to view code image

<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay,
                                TimeUnit unit)

Schedules a value-returning one-shot task that becomes enabled after the given delay. The method returns a ScheduledFuture<V> object which can be used to extract the result computed by the scheduled task or to cancel the scheduled task.

Example 23.3 illustrates scheduling one-shot Runnable and Callable<V> tasks for execution using a scheduled executor service. The example uses a scheduled thread pool with two threads that is created at (1) by calling the newScheduledThreadPool() factory method of the Executors class.

The Runnable task defined at (2) is passed to the schedule() method of the scheduled executor service at (3) to be executed once after a delay of 500 milliseconds. The get() method invoked on the resulting ScheduledFuture<?> object returns the value null, as expected, after execution of the Runnable task to indicate successful completion.

The Callable<String> task defined at (4) is passed to the schedule() method of the scheduled executor service at (5) to be executed once after a delay of one second. In this case, the get() method invoked on the resulting ScheduledFuture<String> object returns a String value after successful completion of the task.

Example 23.3 Executing Scheduled One-Shot Tasks

Click here to view code image

package executors;
import java.util.concurrent.*;
public class ScheduledOneShotTasks {
  public static void main(String[] args) {
    ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);  // (1)
    try {
      // Schedule a one-shot Runnable:
      Runnable runnableTask                                              // (2)
          = () -> System.out.println(“I am a one-shot Runnable task.”);
      ScheduledFuture<?> scheduledRunnableTaskFuture                     // (3)
          = ses.schedule(runnableTask, 500, TimeUnit.MILLISECONDS);
      System.out.println(“Runnable result: ” + scheduledRunnableTaskFuture.get());
      // Schedule a one-shot Callable<String>:
      Callable<String> callableTask
          = () -> “I am a one-shot Callable task.”;                      // (4)
      ScheduledFuture<String> scheduledCallableTaskFuture
          = ses.schedule(callableTask, 1, TimeUnit.SECONDS);             // (5)
      System.out.println(“Callable result: ” + scheduledCallableTaskFuture.get());
    } catch (InterruptedException | ExecutionException exc) {
      exc.printStackTrace();
    } finally {
      ses.shutdown();
    }
  }
}

Probable output from the program:

Click here to view code image

I am a one-shot Runnable task.
Runnable result: null
Callable result: I am a one-shot Callable task.