Java Timer示例
时间:2020-02-23 14:35:16 来源:igfitidea点击:
定时器是一个实用程序类,可用于在特定时间或者重复上安排任务。
让我们说,我们正在开发银行申请,需要在每天晚上7点处理数据。
我们可以使用Timer类轻松安排任务。
为了创建任务,我们需要扩展时间阵列,计时器可用于设置任务的时间。
timertask:它是任务将在特定时间执行。
计时器:它是实用类,用于设置任务的时间。
定时器类是线程安全,所以多个线程可以共享一个计时器对象。
Timer类内部使用java.util.TaskQueue来在内部管理任务。
只有一个线程一次可以执行计时器任务。
例如:
我们正在执行每5秒的任务,但实际执行本身需要10秒钟,然后将添加后续任务将添加到队列中,一旦当前执行完成,它将通知,另一个线程将开始执行任务。
调试计时器对象时,它看起来像这样。
调度任务的方法:
定时器类中有几种方法可以用于计划。
一次执行:
public void schedule(TimerTask task, long delay,long period) For example: //it will execute timer task after delay of 5 seconds timer.schedule(new MyTimerTask(),5000);
对于重复执行:
public void schedule(TimerTask task,long delay) For example: //it will execute timer task in every 2 seconds after first delay of 1 second timer.schedule(new MyTimerTask(),1000,2000);
用于在特定时间执行任务:
public void schedule(TimerTask task, Date time) For example: //Scheduling task at today : 23:13:00 PM Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 13); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); timer.schedule(new MyTimerTask(timer), time);
重复执行特定时间的任务:
public void schedule(TimerTask task, Date firstTime, long period)
例子:
只执行一次:
创建一个类mytimertask.java
package org.igi.theitroad.timer; import java.util.Timer; import java.util.TimerTask; public class MyTimerTask extends TimerTask{ Timer timer; public MyTimerTask(Timer timer) { this.timer=timer; } @Override public void run() { System.out.println("Executing timer task"); //to stop timer thread timer.cancel(); } }
在上面的类中,我们使用Timer.Cancel()来停止定时器线程。
调用Timer.Cancel()方法时,它完成了当前线程的执行,并丢弃队列中的所有其他线程。
创建一个类timermain.java。
package org.igi.theitroad.timer; import java.util.Timer; public class TimerMain { public static void main(String[] args) { //It will create new thread Timer timer=new Timer(); // task will be scheduled after 5 sec delay timer.schedule(new MyTimerTask(timer), 5000); } }
运行上面的程序时,我们将获取以下输出:
Executing timer task
对于重复执行:
创建一个类mytimertask.java
package org.igi.theitroad.timer; import java.util.TimerTask; public class MyTimerTask extends TimerTask{ @Override public void run() { System.out.println("Executing timer task on "+TimerMain.convertTimeToDateFormat(System.currentTimeMillis())); } }
创建一个类timermain.java。
package org.igi.theitroad.timer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; public class TimerMain { public static void main(String[] args) { System.out.println("Timer tasks started on "+convertTimeToDateFormat(System.currentTimeMillis())); System.out.println("**"); //It will create new thread Timer timer=new Timer(); timer.schedule(new MyTimerTask(), 1000,2000); //Stopping the timer thread after some time for example :12 secs. try { Thread.sleep(12000); } catch (InterruptedException e) { e.printStackTrace(); } //you can call timer.cancel() or System.exit(0) System.out.println("**"); System.out.println("Timer tasks finished on "+convertTimeToDateFormat(System.currentTimeMillis())); System.exit(0); } public static String convertTimeToDateFormat(long milliseconds) { SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); Date resultdate = new Date(milliseconds); return sdf.format(resultdate); } }
运行上面的程序时,我们将获取以下输出:
Timer tasks started on Aug 18,2014 22:48:22 ** Executing timer task on Aug 18,2014 22:48:23 Executing timer task on Aug 18,2014 22:48:25 Executing timer task on Aug 18,2014 22:48:27 Executing timer task on Aug 18,2014 22:48:29 Executing timer task on Aug 18,2014 22:48:31 Executing timer task on Aug 18,2014 22:48:33 ** Timer tasks finished on Aug 18,2014 22:48:34
用于在特定时间执行任务:
创建一个类mytimertask.java
package org.igi.theitroad.timer; import java.util.Timer; import java.util.TimerTask; public class MyTimerTask extends TimerTask{ Timer timer; MyTimerTask(Timer timer) { this.timer=timer; } @Override public void run() { System.out.println("Executing timer task on "+TimerMain.convertTimeToDateFormat(System.currentTimeMillis())); //to stop time thread timer.cancel(); } }
创建一个类timermain.java。
package org.igi.theitroad.timer; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; public class TimerMain { public static void main(String[] args) { System.out.println("Timer tasks started on "+convertTimeToDateFormat(System.currentTimeMillis())); System.out.println("**"); //It will create new thread Timer timer=new Timer(); //Scheduling task at today : 23:13:00 PM Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 13); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); timer.schedule(new MyTimerTask(timer), time); } public static String convertTimeToDateFormat(long milliseconds) { SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); Date resultdate = new Date(milliseconds); return sdf.format(resultdate); } }
运行上面的程序时,我们将获取以下输出:
Timer tasks started on Aug 18,2014 23:12:40 ** Executing timer task on Aug 18,2014 23:13:00