Timer timer in Java

Source: Internet
Author: User
Tags dateformat

The timer is primarily used in Java threads to run tasks at specified times or cycles, which are thread-safe, but do not provide real-time (real-time) guarantees.

  

The concept of the daemon thread is mentioned above.

Java is divided into two types of threads: the user thread and the daemon thread.

A daemon thread is a thread that provides a generic service in the background while the program is running, such as a garbage collection thread that is a competent guardian, and that thread is not an integral part of the program. Therefore, when all non-daemon threads end, the program terminates and kills all the daemon threads in the process. Conversely, the program will not terminate as long as any non-daemon threads are still running.

The only difference between the daemon thread and the user thread is the departure of the virtual machine: If the user thread is all out of operation and only the daemon thread is present, the virtual machine exits. Because there is no guardian, the daemon will have no work to do, there is no need to continue to run the program.

Converting a thread to a daemon can be done by invoking the Setdaemon (true) method of the Thread object. There are a few things to keep in mind when using a daemon thread:

(1) Thread.setdaemon (true) must be set before Thread.Start () or run out of a illegalthreadstateexception exception. You cannot set a running regular thread as a daemon thread.

(2) The new thread generated in the daemon thread is also daemon.

(3) The daemon should never access an intrinsic resource, such as a file or a database, because it will be interrupted at any time, even in the middle of an operation.

  

We'll then write a case where the program runs for 3 seconds and then prints out the "Wake up" button on the console.

Package Com.itszt.test7;import Java.text.simpledateformat;import Java.util.timer;import java.util.TimerTask;/** * Java Timer */public class Timertest {    static String str= "HH:mm:ss";    static SimpleDateFormat DateFormat = new SimpleDateFormat (str);    public static void Main (string[] args) {        Timer timer = new timer ();        String Now1 = Dateformat.format (System.currenttimemillis ());        System.out.println (NOW1);        3 seconds delay to execute task        timer.schedule (New MyTask (), 3000),//unit is milliseconds    }}class mytask extends timertask{    @Override Public    void Run () {        System.out.println ("The time to get Up");        String now2 = TimerTest.dateFormat.format (System.currenttimemillis ());        System.out.println (NOW2);    }}

After the above code is executed, the delay is 3 seconds to print out "It's Up" as follows:

21:26:18 It's time to get up 21:26:21

Other ways to Timer:

Schedule (timertask task, date time) performs a timertask task on the specified date, and executes immediately if the date is earlier than the current one.

Schedule (timertask task, long delay, long period) is based on the current time, delays the specified millisecond, and then executes the TimerTask task indefinitely at the specified time interval.

Schedule (timertask task, date firsttime, long period) executes the TimerTask task indefinitely, at a specified interval, after the specified date.

Scheduleatfixedrate (timertask task, long delay, long period) is based on the current time, delays the specified millisecond, and then periodically executes the TimerTask task indefinitely, at the specified time interval.

Timer timer in Java

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.