Java Timer在特定时间停止进程

时间:2012-08-02 09:50:15

标签: java timer scheduled-tasks quartz-scheduler

如何在特定时间停止java中的计时器?

1 个答案:

答案 0 :(得分:1)

Timer particularTimer = new Timer();
    final Timer certainTimer = new Timer();//u can declare a final timer for certain process
    particularTimer.schedule(new TimerTask() {
        public void run(){
            System.out.println("run particularTimer");
            certainTimer.schedule(new TimerTask() {
                public void run() {
                    System.out.println("run certainTimer");
                }
            }, 5000);// it's my start time in milliseconds

        }
    }, 5000);

    //eg
    Thread.sleep(6000);
    particularTimer.cancel();//when u cancel the particularTimer,and u can cancel the certainTimer too.
    certainTimer.cancel();