• 使用ScheduledExecutorService实现Timer


    大家都说Timer不太好用,经常会遇到:如果前边的一个任务比较慢,超出了period,此时timer的下一次轮询也会延迟。

    同事说ScheduleExecutorService可以避免该问题,我写个例子测试下:

    package com.dx.testparallel;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;;
    
    public class TestTimer {
        public static void main(String[] args){
            ScheduledExecutorService scheduledExecutorService=Executors.newScheduledThreadPool(1);
            //Creates and executes a periodic action that becomes enabled 
            //first after the given initial delay, and subsequently with the 
            //given period; that is executions will commence after 
            //initialDelay then initialDelay+period, then initialDelay + 2 * 
            //period, and so on. If any execution of the task encounters an 
            //exception, subsequent executions are suppressed. 
            //Otherwise, the task will only terminate via cancellation or 
            //termination of the executor. If any execution of this task 
            //takes longer than its period, then subsequent executions may 
            //start late, but will not concurrently execute.
            scheduledExecutorService.scheduleAtFixedRate(new TheTask(), 0,1000, TimeUnit.MILLISECONDS);
        }
    }
    package com.dx.testparallel;
    
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;
    
    import javax.lang.model.element.VariableElement;
    
    public class TheTask implements Runnable{    
        @Override
        public void run() {        
              Timestamp ts = new Timestamp(System.currentTimeMillis());  
              UUID uuid = UUID.randomUUID();   
              System.out.println("task"+uuid+" start:"+ts.toString());
            
    //          long num=0;
    //          for(int a=0;a<100000;a++){
    //              for(int i=0;i<1000000;i++){
    //                  for(int j=0;j<1000000;j++){
    //                      num= a*i*j;
    //                  }
    //              }
    //          }
              try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              
              ts = new Timestamp(System.currentTimeMillis());  
              System.out.println("task"+uuid+" end:"+ts.toString());
        }
    }

    测试结果:为什么每次间隔时间是2s,而不是1s.

    task418d088e-3288-4736-b40a-f3c4f496e2a2 start:2016-09-07 00:49:03.699
    task418d088e-3288-4736-b40a-f3c4f496e2a2 end:2016-09-07 00:49:05.836
    task741d2176-dbef-4f12-a73e-13a04ed5eb18 start:2016-09-07 00:49:05.836
    task741d2176-dbef-4f12-a73e-13a04ed5eb18 end:2016-09-07 00:49:07.836
    task96ebb6ca-c99b-482e-b608-bd560e89c7eb start:2016-09-07 00:49:07.836
    task96ebb6ca-c99b-482e-b608-bd560e89c7eb end:2016-09-07 00:49:09.837
    task1ce9f4bb-dd9e-4510-a697-73daef02fe6b start:2016-09-07 00:49:09.837
    task1ce9f4bb-dd9e-4510-a697-73daef02fe6b end:2016-09-07 00:49:11.837
    task0386a855-3e85-4d06-9d91-24abde228731 start:2016-09-07 00:49:11.837
    task0386a855-3e85-4d06-9d91-24abde228731 end:2016-09-07 00:49:13.838
    task886fe541-92d1-49bc-b5bd-2c425cf773ba start:2016-09-07 00:49:13.838
    task886fe541-92d1-49bc-b5bd-2c425cf773ba end:2016-09-07 00:49:15.838
    task460137d1-7daf-419b-b0d5-87f31833176c start:2016-09-07 00:49:15.838
    task460137d1-7daf-419b-b0d5-87f31833176c end:2016-09-07 00:49:17.838
    taskce78bffb-8151-46e8-81d9-57b01e9294a2 start:2016-09-07 00:49:17.838
    taskce78bffb-8151-46e8-81d9-57b01e9294a2 end:2016-09-07 00:49:19.838
    task1fad4b4f-4143-4098-943a-c894ad96ae58 start:2016-09-07 00:49:19.838
    task1fad4b4f-4143-4098-943a-c894ad96ae58 end:2016-09-07 00:49:21.838
    taskc5de2fab-0688-4410-9408-b0990cc76223 start:2016-09-07 00:49:21.838
    taskc5de2fab-0688-4410-9408-b0990cc76223 end:2016-09-07 00:49:23.838
    task7dccb95b-6088-4a5d-a2e0-edb4381f7d0c start:2016-09-07 00:49:23.838
    task7dccb95b-6088-4a5d-a2e0-edb4381f7d0c end:2016-09-07 00:49:25.838
    taskc05908b5-1bb2-496a-9ddc-c3939d783f2c start:2016-09-07 00:49:25.838
    taskc05908b5-1bb2-496a-9ddc-c3939d783f2c end:2016-09-07 00:49:27.839
    task251c4102-f214-4eb9-9e13-7ca7f2eb3362 start:2016-09-07 00:49:27.839
    task251c4102-f214-4eb9-9e13-7ca7f2eb3362 end:2016-09-07 00:49:29.839
    task645f6f0f-9d11-4545-be12-1feab67a6b06 start:2016-09-07 00:49:29.839
    task645f6f0f-9d11-4545-be12-1feab67a6b06 end:2016-09-07 00:49:31.84
    task82da54a5-6620-46f4-a840-10633555d2e8 start:2016-09-07 00:49:31.84
    task82da54a5-6620-46f4-a840-10633555d2e8 end:2016-09-07 00:49:33.84
    taskb33558c1-cb84-4724-85af-ac8060b169a0 start:2016-09-07 00:49:33.84
  • 相关阅读:
    Silverlight 4 新特性之NotificationWindow
    如何理解JavaScript原型
    惹恼程序员的十件事
    浅谈HTTP中Get与Post的区别
    asp中Access与Sql Server数据库区别总结
    SQL208语句
    jQuery源码分析
    3. 在 as 和 强制类型转换之间,优先使用 as 操作符。
    揭秘10项必学的.NET技术
    如何设置远程访问SQL Server2005
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/5847779.html
Copyright © 2020-2023  润新知