Getting Started with Java-timed scheduling

Source: Internet
Author: User

Getting Started with Java-timed scheduling Timer class

The Timer class is a threading facility that can be used to schedule a task to be executed once or periodically after a certain time or a certain period of time, which is used in conjunction with Timetask.

Each timer object corresponds to a thread, so the task performed by the timer should be completed quickly, and no one may delay the execution of subsequent tasks.

Serial number Method Type Describe
1 Timer () Structure Create a timer, and start the timer
2 Cancle () Ordinary Terminates the timer and discards all scheduled tasks
3 Purge () Ordinary Remove a canceled task, which is typically used to free up memory space
4 Schedule (TimerTask task,date time) Ordinary Schedule a task to execute at a specified time
5 Schedule (TimerTask task,date Firsttime,long period) Ordinary Schedule a task to execute at a specified time and then repeat it at a fixed frequency
6 Schedule (TimerTask task,long Delay,long period) Ordinary Schedule a task to be specified after a period of time and repeat at a fixed frequency
7 Scheduleatfixedrate (TimerTask task,date Firsttime,long period) Ordinary Schedule a task to execute at a specified time and then repeat at an approximate fixed frequency
8 Scheduleatfixedrate (TimerTask task,long Delay,long period) Ordinary Schedule a task to execute after a period of time and then repeat at an approximate fixed frequency

TimerTask class

is an abstract class that requires the creation of an abstract method in which subclasses are implemented.

Serial number Method Type Describe
1 Cancle () Ordinary Used to terminate this task, if the task is executed only once and has not been executed, it will never be executed, and if it is executing, it will no longer execute and will not be executed again if the task is repeatedly executed.
2 Run () Ordinary The specific action to be performed by the task, the method that is introduced in the interface runnable, the subclass needs to inherit this method
3 Scheduled ExecutionTime () Ordinary Returns the most recent time to perform the task, typically called in the Run () method, to determine whether there is currently enough time to perform the task
Example

The current time of the scheduled print system.

/* Summary, you need to create a Task object, set up a Timer object * Task class inheritance with an abstract class TimerTask, this abstract class to implement the Run method. */package sep27;import java.util.*;import java.text.*;p ublic class MyTask {public static void main (string[] args) {Timer T =new Timer (); Texttask testtask=new texttask (); T.schedule (Testtask, 1000, 2000);}} Class Texttask extends Timertask{public void run () {SimpleDateFormat sdf1=null;sdf1=new SimpleDateFormat ("yyyy year MM DD Day HH:mm:ss. SSS "); System.out.println ("Current time is:" +sdf1.format (New Date ()));}}

Results:

Current time: September 27, 2016 16:09:14.493 current time is: September 27, 2016 16:09:16.231 current time is: September 27, 2016 16:09:18.231 current time is: September 27, 2016 16:09:20.236 Current time: September 27, 2016 16:09:22.238 current time is: September 27, 2016 16:09:24.242 current time is: September 27, 2016 16:09:26.245 Current time: September 27, 2016 16:09:28.247

  

Getting Started with Java-timed scheduling

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.