zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    使用Docker搭建Wordpress
    Django开发Web页面html增加判断展示
    Python如何取小数点位数
    html内容可编写
    MySQL 设置 非空约束依然能插进去的办法
    Git拉取远程所有分支
    my.ini 配置文件
    node
    TP upload 上传根目录不存在
    linux 安装 memcache
  • 原文地址:https://www.cnblogs.com/redhat0019/p/8044394.html
Copyright © 2011-2022 走看看