• java定时器


     1 package unit;
     2 
     3 import java.util.concurrent.Executors;
     4 import java.util.concurrent.ScheduledExecutorService;
     5 import java.util.concurrent.ScheduledFuture;
     6 import java.util.concurrent.TimeUnit;
     7 
     8 public class Test {
     9     
    10     ScheduledFuture<?> wanRateMonitorFuture = null;
    11     ScheduledExecutorService wanRateMonitorservice = null;
    12      public Test() {
    13             wanRateMonitorservice = Executors.newSingleThreadScheduledExecutor(); 
    14         }
    15     
    16     public static void main(String[] args) {
    17         new Test().startMonitorWanRate();
    18     }
    19     
    20     public void startMonitorWanRate(){
    21 
    22         try {
    23             Runnable runnable = new Runnable() {  
    24                 public void run() {  
    25                     System.out.println("===============================");
    26                 }  
    27             }; 
    28 
    29             if(wanRateMonitorservice == null){
    30                 wanRateMonitorservice = Executors.newSingleThreadScheduledExecutor();  
    31             }
    32             wanRateMonitorFuture = wanRateMonitorservice.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
    33         }catch (Exception e) {
    34             e.printStackTrace();
    35         }  
    36         System.out.println("startMonitorWanRate started!!");
    37     }
    38     
    39     
    40     public void stopMonitorWanRate(){
    41         if(wanRateMonitorFuture != null){
    42             if(wanRateMonitorFuture.cancel(true) == false){
    43                 System.out.println("stopMonitorWanRate Error, wanRateMonitorFuture cann't be cancel");                         
    44             }
    45         } 
    46         if((wanRateMonitorservice != null) && (false == wanRateMonitorservice.isShutdown())){
    47             wanRateMonitorservice.shutdownNow();
    48         }
    49     }
    50 }
  • 相关阅读:
    STL常用容器☞String容器
    初识STL
    函数模板
    多态
    运算符重载
    友元
    对象的初始化和清理
    C++内存分区模型
    传值和传地址
    const的使用
  • 原文地址:https://www.cnblogs.com/redhat0019/p/8044394.html
Copyright © 2020-2023  润新知