schedule方法(首次执行延迟delay秒执行)
package com.glch.threads;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* @author zzl
* @Date 2021/9/16
* @description 测试周期性线程池ScheduledThread-schedule方法
*/
public class ScheduledThreadTest {
public static void main(String[] args) throws Exception {
ScheduledExecutorService executorService = ScheduledThread.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 延迟2秒执行下一个任务
System.out.println("当前时间=" + sdf.format(new Date()));
for (int i = 0; i < 9; i++) {
int finalI = i;
ScheduledFuture future = executorService.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
int value = finalI;
System.out.println("时间=" + sdf.format(new Date()) + ",线程=" + Thread.currentThread().getName() + ",任务=" + value);
// Thread.sleep(3);
return "call";
}
}, 2, TimeUnit.SECONDS);
System.out.println(future.get());
}
executorService.shutdown();
}
}
scheduleAtFixedRate方法
package com.glch.threads;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author zzl
* @Date 2021/9/16
* @description 测试周期性线程池ScheduledThread-scheduleAtFixedRate方法
* initialDelay 表示首次延迟2秒执行
* period 表示周期执行的时间为6秒,即表示会重复执行,重复执行的间隔时间为6秒
* scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,
* 如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。
*/
public class ScheduledThreadTest2 {
public static void main(String[] args) throws Exception {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间="+sdf.format(new Date()));
// 延迟2秒执行下一个任务
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("begin=" + sdf.format(new Date()));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end=" + sdf.format(new Date()));
}
}, 2, 4, TimeUnit.SECONDS);
}
// 周期性执行
// 当执行时间小于period时,下次执行时间= 当前执行开始时间+period
// 当前时间=2021-09-16 05:21:59
// begin=2021-09-16 05:22:01
// end=2021-09-16 05:22:05
// begin=2021-09-16 05:22:07
// end=2021-09-16 05:22:11
// 当执行时间大于period时,下次执行时间 = 当前执行结束时间
// 当前时间=2021-09-16 05:23:13
// begin=2021-09-16 05:23:15
// end=2021-09-16 05:23:20
// begin=2021-09-16 05:23:20
// end=2021-09-16 05:23:25
}
scheduleWithFixedDelay方法
package com.glch.threads;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author zzl
* @Date 2021/9/16
* @description 测试周期性线程池ScheduledThread-scheduleWithFixedDelay方法
* scheduleWithFixedDelay,是从上一个任务结束时开始计时,period时间过去后,再次执行下一次任务。
*/
public class ScheduleWithFixedDelayTest {
public static void main(String[] args) throws Exception {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间="+sdf.format(new Date()));
// 延迟2秒执行下一个任务
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("begin=" + sdf.format(new Date()));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end=" + sdf.format(new Date()));
}
}, 2, 2, TimeUnit.SECONDS);
}
// 周期性执行查询
// 执行结果如下:当前时间延迟2秒时间后开始执行线程,下次执行的时间= 当前执行结束时间+period
// 当前时间=2021-09-16 05:13:28
// begin=2021-09-16 05:13:31
// end=2021-09-16 05:13:36
// begin=2021-09-16 05:13:38
// end=2021-09-16 05:13:43
// begin=2021-09-16 05:13:45
// end=2021-09-16 05:13:50
}