📅  最后修改于: 2020-11-15 03:59:11             🧑  作者: Mango
java.util.concurrent.ScheduledThreadPoolExecutor是ThreadPoolExecutor的子类,还可以安排命令在给定的延迟后运行或定期执行。
Sr.No. | Method & Description |
---|---|
1 |
protected Modifies or replaces the task used to execute a callable. |
2 |
protected Modifies or replaces the task used to execute a runnable. |
3 |
void execute(Runnable command) Executes command with zero required delay. |
4 |
boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() Gets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown. |
5 |
boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() Gets the policy on whether to execute existing delayed tasks even when this executor has been shutdown. |
6 |
BlockingQueue Returns the task queue used by this executor. |
7 |
boolean getRemoveOnCancelPolicy() Gets the policy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation. |
8 |
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 setContinueExistingPeriodicTasksAfterShutdownPolicy (boolean value) Sets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown. |
13 |
void setExecuteExistingDelayedTasksAfterShutdownPolicy (boolean value) Sets the policy on whether to execute existing delayed tasks even when this executor has been shutdown. |
14 |
void setRemoveOnCancelPolicy(boolean value) Sets the policy 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 Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. |
17 |
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 |
Submits a Runnable task for execution and returns a Future representing that task. |
下面的TestThread程序显示了在基于线程的环境中ScheduledThreadPoolExecutor接口的用法。
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TestThread {
public 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
public void run() {
beepHandler.cancel(true);
scheduler.shutdown();
}
}, 10, TimeUnit.SECONDS);
}
static class BeepTask implements Runnable {
public void run() {
System.out.println("beep");
}
}
}
这将产生以下结果。
beep
beep
beep
beep