ScheduleExecutorService是ExecutorService的子接口具备延迟执行任务和定时重复执行任务的功能
ScheduledFuture | schedule(Callable callable, long delay, TimeUnit unit) | 创建并执行在给定延迟后启用的 ScheduledFuture。 |
ScheduledFuture<?> | schedule(Runnable command, long delay, TimeUnit unit) | 创建并执行在给定延迟后启用的一次性操作。 |
ScheduledFuture<?> | scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) | 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 |
ScheduledFuture<?> | scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) | 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。 |
schedule
延迟(数量,单位)
/**
* ScheduleExecutorService是ExecutorService的子接口具备延迟执行任务和定时重复执行任务的功能
* 测试ScheduleExecutorService接口中延迟执行任务和定期重复执行任务的功能(可以指定线程数量,或指定线程数量和线程工厂)
* schedule 可以提交的Callable或Runnable类型的参数
*/
public class ScheduleExecutorServiceDemo01 {
public static void main(String[] args) {
//获取一个具备延迟执行任务的线程池对象 线程数量corePoolSize:3
ScheduledExecutorService ses = Executors.newScheduledThreadPool(3);
//创建多个任务对象,提交任务,然后每个任务延迟2s提交 任务1,延迟数量2,延迟单位秒
for (int i=0;i<10;i++) {
ses.schedule(new MyRunnable(i+1), 2, TimeUnit.SECONDS);
}
System.out.println("over");
}
}
class MyRunnable implements Runnable{
private int id;
public MyRunnable(int id) { this.id = id; }
@Override
public void run() {
String name =Thread.currentThread().getName();
System.out.println(name+"执行了任务:"+id);
}
}
scheduleAtFixedRate
间隔的两秒中包括执行的1.5s
/**
* 测试ScheduleExecutorService接口中延迟执行任务和重复执行任务的功能
* 延迟执行scheduleAtFixedRate包含执行时间
*/
public class ScheduleExecutorServiceDemo02 {
public static void main(String[] args) {
//获取一个具备延迟执行任务的线程池对象
ScheduledExecutorService ses = Executors.newScheduledThreadPool(3, new ThreadFactory() {
int n=1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"自定义线程名:"+n++);
}
});
//创建多个任务对象,提交任务,然后每个任务延迟2s提交
//无论任务执行多长时间, 他都是间隔固定的时间去执行我们的任务 初始等待一秒,间隔两秒执行任务,时间单位秒
ses.scheduleAtFixedRate(new MyRunnable2(1),1,2,TimeUnit.SECONDS);
System.out.println("over");
}
}
class MyRunnable2 implements Runnable{
private int id;
public MyRunnable2(int id) {
this.id = id;
}
@Override
public void run() {
String name =Thread.currentThread().getName();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"执行了任务:"+id);
}
}
scheduleWithFixedDelay
执行时间2s+间隔两秒=4s
/**
* 测试ScheduleExecutorService接口中延迟执行任务和重复执行任务的功能
* 延迟执行scheduleWithFixedDelay不包含执行时间
*/
public class ScheduleExecutorServiceDemo03 {
public static void main(String[] args) {
//获取一个具备延迟执行任务的线程池对象
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
int n=1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"自定义线程名:"+n++);
}
});
//创建多个任务对象,提交任务,然后每个任务延迟2s提交
//无论任务执行多长时间, 他都是间隔固定的时间去执行我们的任务 初始延迟一秒,间隔两秒,执行任务,时间单位秒
ses.scheduleWithFixedDelay(new MyRunnable3(1),1,2,TimeUnit.SECONDS);
System.out.println("over");
}
}
class MyRunnable3 implements Runnable{
private int id;
public MyRunnable3(int id) {
this.id = id;
}
@Override
public void run() {
String name =Thread.currentThread().getName();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"执行了任务:"+id);
}
}