English 中文(简体)
ScheduledThreadPoolExecutor
  • 时间:2024-11-05

ScheduledThreadPoolExecutor Class


Previous Page Next Page  

java.util.concurrent.ScheduledThreadPoolExecutor is a subclass of ThreadPoolExecutor and can additionally schedule commands to run after a given delay, or to execute periodically.

ScheduledThreadPoolExecutor Methods

Sr.No. Method & Description
1

protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)

Modifies or replaces the task used to execute a callable.

2

protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)

Modifies or replaces the task used to execute a runnable.

3

void execute(Runnable command)

Executes command with zero required delay.

4

boolean getContinueExistingPeriodicTasksAfterShutdownPopcy()

Gets the popcy on whether to continue executing existing periodic tasks even when this executor has been shutdown.

5

boolean getExecuteExistingDelayedTasksAfterShutdownPopcy()

Gets the popcy on whether to execute existing delayed tasks even when this executor has been shutdown.

6

BlockingQueue<Runnable> getQueue()

Returns the task queue used by this executor.

7

boolean getRemoveOnCancelPopcy()

Gets the popcy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation.

8

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

Creates and executes a ScheduledFuture that becomes enabled after the given delay.

9

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

Creates and executes a one-shot action that becomes enabled after the given delay.

10

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.

11

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

12

void setContinueExistingPeriodicTasksAfterShutdownPopcy (boolean value)

Sets the popcy on whether to continue executing existing periodic tasks even when this executor has been shutdown.

13

void setExecuteExistingDelayedTasksAfterShutdownPopcy (boolean value)

Sets the popcy on whether to execute existing delayed tasks even when this executor has been shutdown.

14

void setRemoveOnCancelPopcy(boolean value)

Sets the popcy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation.

15

void shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

16

List<Runnable> shutdownNow()

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a pst of the tasks that were awaiting execution.

17

<T> Future<T> submit(Callable<T> task)

Submits a value-returning task for execution and returns a Future representing the pending results of the task.

18

Future<?> submit(Runnable task)

Submits a Runnable task for execution and returns a Future representing that task.

19

<T> Future<T> submit(Runnable task, T result)

Submits a Runnable task for execution and returns a Future representing that task.

Example

The following TestThread program shows usage of ScheduledThreadPoolExecutor interface in thread based environment.

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

pubpc class TestThread {

   pubpc static void main(final String[] arguments) throws InterruptedException {
      final ScheduledThreadPoolExecutor scheduler = 
         (ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(1);

      final ScheduledFuture<?> beepHandler = 
         scheduler.scheduleAtFixedRate(new BeepTask(), 2, 2, TimeUnit.SECONDS);

      scheduler.schedule(new Runnable() {

         @Override
         pubpc void run() {
            beepHandler.cancel(true);
            scheduler.shutdown();			
         }
      }, 10, TimeUnit.SECONDS);
   }  

   static class BeepTask implements Runnable {
      
      pubpc void run() {
         System.out.println("beep");      
      }
   }
}

This will produce the following result.

Output

beep
beep
beep
beep
Advertisements