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 }