摘要:

mer=newTimer();DatedateRef=sdf.parse(dateString);System.out.println("字符串时间:"+dateRef.toLocaleString()+"当前时间:"+newDate().toLocaleString());timer.schedule(task,dateRef,4000);}}运行结果如下

Timer

schedule(TimerTask task, Date time)

该方法在指定日期执行任务,如果是过去的时间,这个任务会立即被执行。

执行时间早于当前时间

示例代码,当前时间是2019年9月19日,代码中写的是前一天的时间。

ixedThreadPool适用于为了满足资源管理的需求,而需要限制线程数量的应用场景。

SingleThreadExecutorpublicstaticExecutorServicenewSingle
public class MyTask1 extends TimerTask {
private static Timer timer = new Timer();
public void run()
{
System.out.println("运行了!时间为:" + new Date());
}
public static void main(String[] args) throws Exception
{
MyTask1 task = new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2019-9-18 21:22:00";
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
timer.schedule(task, dateRef);
}
}

运行结果如下

cutor可以在构造函数中指定多个对应的后台线程数。Java提供了多种类型的ScheduledThreadPoolExecutor,可以通过Executors创建,比较常见的有ScheduledThr

以看到,立即执行。执行时间晚于当前时间运行结果如下字符串时间:2019-9-1922:12:00当前时间:2019-9-1922:11:24运行了!时间为:ThuSep1922:12:00CEST20

hreadFactoryBuilder().setNameFormat("xx-task-%d").build();handler:饱和策略。当队列和线程池都满了,说明线程池处于饱

字符串时间:2019-9-18 21:22:00 当前时间:2019-9-19 20:18:26

运行了!时间为:Thu Sep 19 20:18:26 CEST 2019

可以看到,过去的时间立即执行。

执行时间晚于当前时间

修改代码的dateString改为未来的时间,如"2019-9-19 20:22:00"

运行结果如下

t("pool-task-%d").build();staticExecutorServiceexecutor=newThreadPoolExecutor(Runtime.getR

字符串时间:2019-9-19 20:22:00 当前时间:2019-9-19 20:21:22

运行了!时间为:Thu Sep 19 20:22:00 CEST 2019

可以看到,未来的时间要等到目标时间才会执行。

多个Timer同时执行

示例代码如下

"+dateRef.toLocaleString()+"当前时间:"+newDate().toLocaleString());timer.schedule(task,da
public class MyTask2 extends TimerTask {
private static Timer timer = new Timer();
public void run()
{
System.out.println("运行了!时间为:" + new Date());
}
public static void main(String[] args) throws Exception
{
MyTask2 task1 = new MyTask2();
MyTask2 task2 = new MyTask2();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2019-9-19 21:12:00";
String dateString2 = "2019-9-19 21:12:00";
Date dateRef1 = sdf1.parse(dateString1);
Date dateRef2 = sdf2.parse(dateString2);
System.out.println("字符串时间:" + dateRef1.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
System.out.println("字符串时间:" + dateRef2.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
timer.schedule(task1, dateRef1);
timer.schedule(task2, dateRef2);
}
}

运行结果如下

cutor可以在构造函数中指定多个对应的后台线程数。Java提供了多种类型的ScheduledThreadPoolExecutor,可以通过Executors创建,比较常见的有ScheduledThr

以看到,立即执行。执行时间晚于当前时间运行结果如下字符串时间:2019-9-1922:12:00当前时间:2019-9-1922:11:24运行了!时间为:ThuSep1922:12:00CEST20

hreadFactoryBuilder().setNameFormat("xx-task-%d").build();handler:饱和策略。当队列和线程池都满了,说明线程池处于饱

字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57

字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57

运行了!时间为:Thu Sep 19 21:12:00 CEST 2019

运行了!时间为:Thu Sep 19 21:12:00 CEST 2019

说明可以多任务执行。执行时间和当前时间的关系规则同上。

utor(1,1,0L,TimeUnit.MILLISECONDS,newLinkedBlockingQueue()));}SingleThreadExecutor是使

schedule(TimerTask task, Date firstTime, long period)

该方法在指定的时间执行任务,间隔period时间再次执行,无限循环。

执行时间早于当前时间

示例代码

Queue<>(1024),threadFactory,newThreadPoolExecutor.AbortPolicy());publicstaticvoidmain(String[]
public class MyTask3 extends TimerTask {
public void run()
{
System.out.println("运行了!时间为:" + new Date());
}
public static void main(String[] args) throws Exception
{
MyTask3 task = new MyTask3();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2019-09-19 22:01:00";
Timer timer = new Timer();
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
timer.schedule(task, dateRef, 4000);
}
}

运行结果如下

cutor可以在构造函数中指定多个对应的后台线程数。Java提供了多种类型的ScheduledThreadPoolExecutor,可以通过Executors创建,比较常见的有ScheduledThr

以看到,立即执行。执行时间晚于当前时间运行结果如下字符串时间:2019-9-1922:12:00当前时间:2019-9-1922:11:24运行了!时间为:ThuSep1922:12:00CEST20

hreadFactoryBuilder().setNameFormat("xx-task-%d").build();handler:饱和策略。当队列和线程池都满了,说明线程池处于饱

字符串时间:2019-9-19 22:01:00 当前时间:2019-9-19 22:09:13

运行了!时间为:Thu Sep 19 22:09:13 CEST 2019

运行了!时间为:Thu Sep 19 22:09:17 CEST 2019

运行了!时间为:Thu Sep 19 22:09:21 CEST 2019

运行了!时间为:Thu Sep 19 22:09:25 CEST 2019

···

可以看到,立即执行。

执行时间晚于当前时间

运行结果如下

ecutor.execute(()->System.out.println("HelloWorld"));//2.有返回值的任务执行->CallableFuture&l

字符串时间:2019-9-19 22:12:00 当前时间:2019-9-19 22:11:24

运行了!时间为:Thu Sep 19 22:12:00 CEST 2019

运行了!时间为:Thu Sep 19 22:12:04 CEST 2019

运行了!时间为:Thu Sep 19 22:12:08 CEST 2019

运行了!时间为:Thu Sep 19 22:12:12 CEST 2019

运行了!时间为:Thu Sep 19 22:12:16 CEST 2019

···