码迷,mamicode.com
首页 > 编程语言 > 详细

java定时器

时间:2017-12-15 18:55:36      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:int   sch   color   ble   gpo   java   []   err   定时器   

 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 }

 

java定时器

标签:int   sch   color   ble   gpo   java   []   err   定时器   

原文地址:http://www.cnblogs.com/redhat0019/p/8044394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!