- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.Date;
-
-
-
-
- public class TimerTest {
-
- public static void main(String[] args) {
- Timer t = new Timer();
-
-
- t.schedule(new TimerTask(){
- public void run()
- {System.out.println("this is task you do1");}
- },5*1000);
-
-
-
- Date d1 = new Date(System.currentTimeMillis()+1000);
- t.schedule(new TimerTask(){
- public void run()
- {System.out.println("this is task you do2");}
- },d1);
-
-
-
- Date d2 = new Date(System.currentTimeMillis()+1000);
- t.schedule(new TimerTask(){
- public void run()
- {System.out.println("this is task you do3");}
- },d2,1*1000);
-
-
-
- t.schedule(new TimerTask(){
- public void run()
- {System.out.println("this is task you do4");}
- },3*1000,1*1000);
-
-
-
- t.scheduleAtFixedRate(new TimerTask(){
- public void run()
- {System.out.println("this is task you do6");}
- },3*1000,2*1000);
- }
schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule不会把过去的时间算上。
比如
- SimpleDateFormat fTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- Date d1 = fTime.parse("2005/12/30 14:10:00");
-
- t.scheduleAtFixedRate(new TimerTask(){
- public void run()
- {
- System.out.println("this is task you do6");
- }
- },d1,3*60*1000);
间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,如果我在14:17:00分执行这个程序,那么会立刻打印3次
this is task you do6 //14:10
this is task you do6 //14:13
this is task you do6 //14:16
并且注意,下一次执行是在14:19 而不是 14:20。就是说是从指定的开始时间开始计时,而不是从执行时间开始计时。
但是上面如果用schedule方法,间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,那么在14:17:00分执行这个程序,则立即执行程序一次。并且下一次的执行时间是 14:20,而不是从14:10开始算的周期(14:19)。
分享到:
相关推荐
主要为大家详细介绍了java中timer的schedule和scheduleAtFixedRate方法区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java定时任务schedule和scheduleAtFixedRate的异同
NULL 博文链接:https://michael1990.iteye.com/blog/2113141
调度可以用 Timer 【调用 schedule() 或者 scheduleAtFixedRate() 方法实现】或者 ScheduledExecutorService 【结合工作中其它的需求,笔者选用此】 ScheduledExecutorService的初始化(线程池):
最常用的是schedule模式,它可以通过两种方式执行任务:在某个特定时刻开始执行任务;从当前时刻开始,在某个固定时段之后开始执行任务。这两种方式都可以指定任务重复执行的频率。另一种执行任务的模式是...
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { public void run() { Integer number = NumberMachine.getInstance().getCommonManager().generateNewManager(); System.out....
定时删除文件,导入库类有Timer类,TimeTask类,运用的函数有:scheduleAtFixedRate()。
调用scheduleAtFixedRate()方法或者scheduleWithFixedDelay()方法时,会向DelayQueue添加一个实现了RunnableScheduledFuture接口的ScheduledFutureTask
timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Message message=new Message(); message.what=1; handler.sendMessage(message); } }, 1000,5000); 2.在线程中创建Handle
串行执行器服务 ScheduledExecutorService 的测试实用程序实现 允许测试如下代码: ... service.scheduleAtFixedRate(new Runnable() { @Override public void run() { count++; bar.doWhatever(); } },
timer.scheduleAtFixedRate(new TimerTask() { int j=0; int i=0; @Override public void run() { if(over==true){ return; } handler.sendEmptyMessage(kPostInvalidate); i=i+...
scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1,2, TimeUnit.SECONDS); super.onStart(); } @Override protected void onStop() { //当Activity不可见的时候停止切换 ...