Timer defects

Original link: http://www.cnblogs.com/mjorcen/p/4235409.html

  There java.util.Timer timer delays execution management tasks ( "task such as performing post-1000ms") and a periodic execution ( "eg once every 500ms perform this task"). However, Timer, there are some defects, so you should consider using ScheduledThreadPoolExecutor as a substitute, Timer support for scheduling is based on absolute time, rather than relative time, thus the task of changing the system clock is sensitive; ScheduledThreadExecutor only supports relative time .

    Timer Another problem is that, if an exception is thrown TimerTask unchecked, Timer will produce unpredictable behavior. Timer thread does not catch the exception, so TimerTask thrown unchecked exception will terminate the timer thread. In this case, Timer also will not resume the thread of execution; it wrong to think that the whole Timer are canceled. In this case, it has been scheduled but not yet executed TimerTask never again to perform a new task can not be scheduled.

 

public class OutOfTime {
    public static void main(String[] args) throws Exception {
        H h = new Timer ();
        timer.schedule(new ThrowTask(), 1);
        SECONDS.sleep(1);
        timer.schedule(new ThrowTask(), 1);
        SECONDS.sleep(5);
    }

    static class ThrowTask extends TimerTask {
        public void run() {
            /*try {
                 Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace ();
            }*/
            throw new RuntimeException();
        }
    }
}

Results of the

Exception in thread "Timer-0" java.lang.RuntimeException
    at net.jcip.examples.OutOfTime$ThrowTask.run(OutOfTime.java:31)
    and java.util.TimerThread.mainLoop (Timer.java: 555 )
    that java.util.TimerThread.run (Timer.java: 505 )
Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled.
    to java.util.Timer.sched (Timer.java: 397 )
    that java.util.Timer.schedule (Timer.java: 193 )
    at net.jcip.examples.OutOfTime.main(OutOfTime.java:19)

 

Timer single-threaded tasks, task execution time may be lost or inaccurate.

 

  Timer when the mission is create a single thread. If time is a relatively long time to perform the task, then the accuracy of the other task execution time will be affected. For example, every 1 second to perform a task, the middle of a long task execution time over 5 seconds, then during the execution of a task is completed, it will quickly perform tasks or lost four times.

public class OutOfTime {
    public static void main(String[] args) throws Exception {
        H h = new Timer ();
        timer.schedule(new ThrowTask(), 1);
        SECONDS.sleep(1);
        timer.schedule(new ThrowTask(), 1);
        SECONDS.sleep(5);
    }

    static class ThrowTask extends TimerTask {
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            throw new RuntimeException();
        }
    }
}

Results of the

Exception in thread "Timer-0" java.lang.RuntimeException
    at net.jcip.examples.OutOfTime$ThrowTask.run(OutOfTime.java:31)
    and java.util.TimerThread.mainLoop (Timer.java: 555 )
    that java.util.TimerThread.run (Timer.java: 505)

 

Reproduced in: https: //www.cnblogs.com/mjorcen/p/4235409.html

Guess you like

Origin blog.csdn.net/weixin_30501857/article/details/94784829