Java 定时器 5秒钟后自动退出

来源:互联网 发布:淘宝权重怎么做上去 编辑:程序博客网 时间:2024/05/16 18:29
  1. private void autoExit() {   
  2.         Timer timer = new Timer();   
  3.         timer.schedule(new TimerTask() {   
  4.             int MAX_COUNTER = 5;   
  5.             int counter = 0;   
  6.   
  7.             @Override  
  8.             public void run() {   
  9.                 if (MAX_COUNTER == counter) {   
  10.                     if (!isExitCB.isSelected()) {   
  11.                         System.exit(0);   
  12.                     } else {   
  13.                         this.cancel();   
  14.                     }   
  15.                 }   
  16.                 willExitLabel.setText("System will exit within "  
  17.                         + (MAX_COUNTER - counter) + " seconds");   
  18.                 counter++;   
  19.             }   
  20.         }, 01000);   
  21.     }