J2-timer User Guide

Source: Internet
Author: User

There is a new improvement in J2SE 1.3, that is, it provides a Timer class that can implement multi-task scheduling and execution more easily. Scheduling is completed by a background thread.
MIDP also includes this improvement, which benefits the developers of j2s.
J2's prompt shows two classes for defining and debugging tasks: TimerTask and Timer. TimerTask is an abstract base class defined by the user for all tasks to be scheduled. The Timer class is responsible for creating and managing execution threads during task execution.
Define a task, define a subclass of TimerTask, and implement the run method. For example
Import java. util .*;
Public class MyTask extends TimerTask
{
Public void run ()
{
System. out. println ("Running the task ");
}
}
Do you think the run method is very familiar? This is because TimerTask implements the java. lang. Runnable interface.
The Timer class calls this run method to execute each task. Note that the task executed by each run method must be terminated as soon as possible, because each Timer object can only execute one task at a time.
After defining a task, you can generate a Timer object and call the schedule method to schedule it, as shown in the following code:
Import java. util .*;
Timer timer = new Timer ();
TimerTask task = new MyTask ();
// Wait 10 seconds before executing this task...
Timer. schedule (tasks, 10000 );
// Wait 10 seconds before the task is executed, and then execute the task every 10 seconds.
Timer. schedule (task, 5000,100 00 );
The schedule method is reloaded four times. Each task can be executed after a specific time point (specified by a Date object) or a specific delay period (in milliseconds. You can schedule this task to be executed only once or repeatedly within a specific period of time. Timer also provides a scheduleAtFixedRate method to specify the time period for repeated execution based on the first execution time of the task. If a task is delayed, the task that is scheduled to be executed later will be "connected" by the corresponding shortening wait time.
Each Timer object creates and manages a background thread. Generally, it is enough for a program to create a Timer. Of course, you can also create any number of timers as needed. You can also stop a Timer at any time and terminate the background thread by calling the cancel method. However, once Timer is terminated, the execution cannot be resumed unless you regenerate a Timer object and reschedule the task you want to execute. Timer objects are thread-safe. You can directly access Timer objects in a multi-threaded environment without any explicit synchronous processing.

Related Article

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.