java中的Timer TimerTask ScheduledExecutorService

来源:互联网 发布:怎么才能在淘宝上卖东西 编辑:程序博客网 时间:2024/06/06 17:37

Timer和TimerTask

有如下好处:

1.当启动和取消任务时可以控制
2.第一次执行任务时可以指定你想要的delay时间

在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。
Timer实例可以调度多任务,它是线程安全的。
当Timer的构造器被调用时,它创建了一个线程,这个线程可以用来调度任务。
下面是代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.text.DateFormat;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4. import java.util.Timer;  
  5. import java.util.TimerTask;  
  6.   
  7. //用DATE类实现  
  8. public class TimedTask1 {  
  9.   
  10.     // 获取应该在多少秒后  
  11.     public static long getTaskTime(int shi,int fen) {  
  12.         DateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
  13.   
  14.         // 当前时分秒字符串切成数组  
  15.         String[] sArr = sdf.format(new Date()).split(":");  
  16.         // 从数组取值换算成 秒计数值  
  17.         long currentMiao = (Integer.parseInt(sArr[0]) *60*60) + (Integer.parseInt(sArr[1]) *60)  
  18.                 + Integer.parseInt(sArr[2]);  
  19.         // 设定的执行时间换算成 秒计数值  
  20.         long runTime = (shi*60*60 + fen*60);  
  21.   
  22.         if (currentMiao <= runTime) {  
  23.             return runTime - currentMiao;  
  24.         } else {  
  25.             return currentMiao + (24*60*60) - (currentMiao - runTime);  
  26.         }  
  27.     }  
  28.   
  29.     // 定时任务  
  30.     public static void cronJob(int shi,int fen) {  
  31.         Timer timer = new Timer();  
  32.         timer.schedule(new TimerTask() {  
  33.             // 把run方法里的内容换成要运行的代码  
  34.             public void run() {  
  35.                   
  36.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  37.                 System.out.println("当前的系统时间为:" + sdf.format(new Date()));  
  38.                   
  39.             }  
  40.         }, getTaskTime(shi,fen) *100024*60*60*1000);  
  41.     }  
  42.   
  43.     /*public static void main(String[] args) { 
  44.         cronJob(18,30); 
  45.     }*/  
  46. }  
  47.   
  48. // 用Calendar实现  
  49. import java.text.SimpleDateFormat;  
  50. import java.util.Calendar;  
  51. import java.util.Date;  
  52. import java.util.Timer;  
  53. import java.util.TimerTask;  
  54.   
  55. public class TimedTask2 {  
  56.     public static void cronJob(int shi, int fen, int miao) {  
  57.         Calendar cal = Calendar.getInstance();  
  58.         // 每天定点执行  
  59.         cal.set(Calendar.HOUR_OF_DAY, shi);  
  60.         cal.set(Calendar.MINUTE, fen);  
  61.         cal.set(Calendar.SECOND, miao);  
  62.   
  63.         Timer timer = new Timer();  
  64.         timer.schedule(new TimerTask() {  
  65.   
  66.             public void run() {  
  67.                 // 把run方法里的内容换成你要执行的内容  
  68.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  69.                 System.out.println("当前的系统时间为:" + sdf.format(new Date()));  
  70.             }  
  71.         }, cal.getTime(), 24 * 60 * 60 * 1000);  
  72.     }  
  73.   
  74.     /* 
  75.      * public static void main(String[] args) {  
  76.      * cronJob(18, 30, 0); //24小时制 时分秒  
  77.      * } 
  78.      */  
  79. }  


java定时任务接口ScheduledExecutorService  

ScheduledExecutorService是从Java SE 5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。

ScheduledExecutorService,是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。

需要注意,只有当调度任务来的时候,ScheduledExecutorService才会真正启动一个线程,其余时间ScheduledExecutorService都是出于轮询任务的状态。

好处:

1.相比于Timer的单线程,它是通过线程池的方式来执行任务的2.可以很灵活的去设定第一次执行任务delay时间3.提供了良好的约定,以便设定执行的时间间隔。

 

下面是代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. import java.util.concurrent.Executors;  
  4. import java.util.concurrent.ScheduledExecutorService;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. public class TimedTask {  
  8.     public static void main(String[] args) {  
  9.         Runnable runnable = new Runnable() {  
  10.             public void run() {  
  11.                 // 把run方法里的内容换成你要执行的内容  
  12.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.                 System.out.println("当前的系统时间为:" + sdf.format(new Date()));  
  14.             }  
  15.         };  
  16.         ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();  
  17.         //public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);  
  18.         //command--执行的任务,initialDelay--延迟开始,period--间隔时间,unit--时间单位  
  19.         service.scheduleAtFixedRate(runnable, 05, TimeUnit.SECONDS);  
  20.     }  
  21. }