java定时器

Posted 林**

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于java定时器的主要内容,如果未能解决你的问题,请参考以下文章

前端面试题之手写promise

HTML代码片段

HTML代码片段

java代码在片段活动中不起作用

CPNtools协议建模安全分析---实例变迁标记

分享几个实用的代码片段(附代码例子)