Java Timer usage notes, javatimer notes

Source: Internet
Author: User

Java Timer usage notes, javatimer notes

Timer: A thread facility used to schedule tasks to be executed in background threads. You can schedule the task to be executed once, or perform the task repeatedly on a regular basis.

In layman's terms, you can set a time to execute a task or execute a task repeatedly. For example, you can regularly remind the user to activate the software or terminate the continued use of the software.

TimerTask: a task scheduled by Timer for one execution or repeated execution.

This class implements the Runnable interface

 

Column:

 

1. output the current time every second

(1) first create a Timer object. Here the Timer (String name) constructor is used.

(2) Use Calendar to create a start time

(3) Call the schedule method of the Timer class to execute the task. If the Date time parameter is earlier than the current time, it is executed immediately. (Note: Because the schedule method has many reloads, I will not list them here. You can view the API documentation on your own)

(4) override the Run method in TimerTask

Public class TimerTest {public static void main (String [] args) {/** create a Timer object: A thread facility used to schedule tasks to be executed in background threads. You can schedule the task to be executed once, or perform the task repeatedly on a regular basis.
**/Timer tm = new Timer ("MyThread");/** schedule the specified task */Calendar cal = Calendar. getInstance (); cal. set (2015, 1, 1, 13, 40, 00); tm. schedule (new TimerTask () {@ Overridepublic void run (){
// Name of the output thread, whose value is MyThread
System. err. println (Thread. currentThread (). getName (); while (true) {try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();}/** output time per second */SimpleDateFormat sdf = new SimpleDateFormat ("YYYY-MM-DD hh: mm: ss"); String time = sdf. format (new Date (); System. err. println (time) ;}}, cal. getTime (), 1000 );}}

2. daemon thread

It mainly explains

(1) to enable the daemon thread, set it to true first.

(2) the life cycle of the daemon thread. When the current thread has ended, the daemon thread also ends, and the content in it will not be executed.

(3) In the execution result, afeter sleep... will not be executed. Because sleep causes this thread to give up CPU usage

(4) The input time parameter is new Date (), indicating immediate execution. Therefore, the name of the daemon thread and Before sleep ...... will be printed,

If the input parameter is 1000, that is, 1 second, the name of the daemon thread and Before sleep ...... it will not be printed, because the JVM already thinks there is no thread to execute.

Public class TimerDaemon_1 {public static void main (String [] args) {// print the main thread name System. out. println (Thread. currentThread (). getName () + "............ "); // The default value is false final Timer tr = new Timer (" MyThread ", true); tr. schedule (new TimerTask () {@ Override public void run () {System. out. println (Thread. currentThread (). getName (); System. out. println ("Before sleep ............ "); try {Thread. sleep (1000); // here let the thread sleep for 1 second} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("After sleep .............. ") ;}// the new Date () indicates immediate execution}, new Date ());}}

 

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.