Java Timer, TimerTask, Timer.schedule

Source: Internet
Author: User

The meaning of schedule (timetable, schedule)

Timer.schedule (New TimerTask () {void run ()},0, 60*60*1000);
Timer.schedule (New MyTask (Event.getservletcontext ()), 0, 60*60*1000);
First parameter "New MyTask (Event.getservletcontext ())":
is the TimerTask class, in the package: import Java.util.TimerTask. The consumer inherits the class and implements the public void run () method, because the TimerTask class implements the Runnable interface.

The second parameter "0" means: (0 means no delay)
When you call this method, the method is bound to call the run () method in the TimerTask class TimerTask class, which is the difference between the time between the two, that is, after the user calls the schedule () method, it takes such a long time to execute the run () party for the first time. Method.

The third parameter, "60*60*1000", means:
(Unit is in milliseconds 60*60*1000 for one hour)
(in milliseconds 3*60*1000 to three minutes)
After the first call, the run () method is called once every number of times from the second start.

Https://www.cnblogs.com/lcngu/p/5010377.html

Add:

Is there a problem with schedule multiple tasks with the same timer?

A Timer object in Java itself has only one thread, and if multiple tasks are submitted to him, and a task is time-consuming, the other task will still wait until the execution is complete, even when the task is executed. More problematic, if the previous task throws an exception that causes the thread to terminate, the subsequent task cannot execute.

Java5 recommend the use of Java.util.concurrent scheduledexecutorservice, at least to avoid the problem that causes the thread to end.

If you need more accurate time, be sure to handle a task with a scheduledexecutorservice.

Another said that daily scheduled tasks, with the Windows Task plan, Linux Crond execute a Java application can be implemented, why use the Java timer to keep the process resident, the longer the operation.

Java Timer, TimerTask, Timer.schedule

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.