Java 定时器 5秒钟后自动退出 博客分类: SorLib javaTimer定时器退出
程序员文章站
2024-03-11 23:51:38
...
一个简易的Java定时器方法, 5秒钟后退出。
private void autoExit() { Timer timer = new Timer(); timer.schedule(new TimerTask() { int MAX_COUNTER = 5; int counter = 0; @Override public void run() { if (MAX_COUNTER == counter) { if (!isExitCB.isSelected()) { System.exit(0); } else { this.cancel(); } } willExitLabel.setText("System will exit within " + (MAX_COUNTER - counter) + " seconds"); counter++; } }, 0, 1000); }