public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService {
public interface ScheduledExecutorService extends ExecutorService {
ScheduledExecutorService多了四个延时或周期任务接口
//达到给定的延时时间后,执行任务。这里传入的是实现Runnable接口的任务,
//因此通过ScheduledFuture.get()获取结果为null
public ScheduledFuture> schedule(Runnable command,
long delay, TimeUnit unit);
//达到给定的延时时间后,执行任务。这里传入的是实现Callable接口的任务,
//因此,返回的是任务的最终计算结果
public ScheduledFuture schedule(Callable callable,
long delay, TimeUnit unit);
//是以上一个任务开始的时间计时,period时间过去后,
//检测上一个任务是否执行完毕,如果上一个任务执行完毕,
//则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行
public ScheduledFuture> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
//当达到延时时间initialDelay后,任务开始执行。上一个任务执行结束后到下一次
//任务执行,中间延时时间间隔为delay。以这种方式,周期性执行任务。
public ScheduledFuture> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
System.out.println("main " + new Date());
ScheduledFuture> schedule = pool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("schedule Runnable " + new Date());
}
}, 1, TimeUnit.SECONDS);
System.out.println(schedule.get());
ScheduledFuture schedule1 = pool.schedule(new Callable() {
@Override
public Integer call() throws Exception {
System.out.println("schedule Callable " + new Date());
return 1;
}
}, 2, TimeUnit.SECONDS);
System.out.println(schedule1.get());
}
main Wed Sep 04 11:07:42 CST 2019
schedule Runnable Wed Sep 04 11:07:43 CST 2019
null
schedule Callable Wed Sep 04 11:07:45 CST 2019
1
延时1秒后,执行第一次任务,周期2秒过后,再次执行任务
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
System.out.println("main " + new Date());
ScheduledFuture> schedule = pool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("schedule Runnable " + new Date());
}
}, 1, 2, TimeUnit.SECONDS);
System.out.println(schedule.get());
}
main Wed Sep 04 11:14:06 CST 2019
schedule Runnable Wed Sep 04 11:14:07 CST 2019
schedule Runnable Wed Sep 04 11:14:09 CST 2019
schedule Runnable Wed Sep 04 11:14:11 CST 2019
如果任务时间3秒,大于周期时间2秒,等待任务执行完毕后,立即再次执行任务
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
System.out.println("main " + new Date());
ScheduledFuture> schedule = pool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("schedule Runnable " + new Date());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 1, 2, TimeUnit.SECONDS);
System.out.println(schedule.get());
}
main Wed Sep 04 11:16:12 CST 2019
schedule Runnable Wed Sep 04 11:16:13 CST 2019
schedule Runnable Wed Sep 04 11:16:16 CST 2019
schedule Runnable Wed Sep 04 11:16:19 CST 2019
等待任务执行完毕后,延时2秒,再次执行任务
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
System.out.println("main " + new Date());
ScheduledFuture> schedule = pool.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("schedule Runnable " + new Date());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 1, 2, TimeUnit.SECONDS);
System.out.println(schedule.get());
}
main Wed Sep 04 11:18:11 CST 2019
schedule Runnable Wed Sep 04 11:18:12 CST 2019
schedule Runnable Wed Sep 04 11:18:17 CST 2019
schedule Runnable Wed Sep 04 11:18:22 CST 2019