CodeGym /Java Course /Module 2. Java Core /newScheduledThreadPool method

newScheduledThreadPool method

Module 2. Java Core
Level 14 , Lesson 5
Available

Another type of task pool is the scheduled task pool. Judging by the name, we can assume that we use this type to schedule when a particular task should be launched.

This type of service is useful when we have a task to launch an activity after some time has elapsed or we want to schedule a recurring task.

To use one, we call Executors.newScheduledThreadPool(1).

We'll talk about the parameters a little later. But for now, what we need to know is that when this method is called we get a ScheduledExecutorService object.

ScheduledExecutorService is an interface that extends ExecutorService.

The following methods appear in this interface:

Method Explanation

ScheduledFuture<?>
schedule(Runnable command,
                                  long delay, TimeUnit unit);
Creates and executes a one-time action that runs after the specified delay.

<V> ScheduledFuture<V>
schedule(Callable<V> callable,
                                      long delay, TimeUnit unit);
Creates and executes a ScheduledFuture object that executes after a specified delay.

ScheduledFuture<?>
scheduleAtFixedRate(Runnable command,
                                             long initialDelay,
                                             long period,
                                             TimeUnit unit);
Creates and executes a recurring action that runs first after the specified initial delay and then again after the specified period. In other words, execution will start after initialDelay, then initialDelay + period, then initialDelay + 2 * period, and so on.

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                long initialDelay,
                                                long delay,
                                                TimeUnit unit);
Creates and executes a recurring action that executes first after the specified initial delay, and then again with the specified delay between the completion of one execution and the start of the next.

So the interface lets us run tasks at regular intervals or after some delay.

More on the newScheduledThreadPool method.

We can call it in several ways:


newScheduledThreadPool(int corePoolSize)
corePoolSize is the number of threads to keep in the pool, even if they are idle.

newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

corePoolSize is the number of threads to keep in the pool, even if they are idle.

threadFactory is the factory to use when creating new threads.

Both methods will create a thread pool that can schedule actions to execute after a specified delay or periodically.

Let's look at an example to see how ScheduledThreadPool works.

For example, suppose we have a task to check email every 5 seconds, but this check must not affect the main program and entails the potential consumption of additional resources.

We have a task class that simulates checking email.


public class Task implements Runnable {
   @Override
   public void run() {
       System.out.println("Checking email...");
   }
}

Next, we create a thread pool and schedule the check.


ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
executorService.scheduleAtFixedRate(new Task(), 0, 5, TimeUnit.SECONDS);

In the output, every 5 seconds we see:

Checking email...

In general, we can use such a pool to perform periodic "housekeeping" tasks, as in the example. Housekeeping tasks are tasks that must be performed regardless of what the main program is doing.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION