Timer timer in Java

  Timer is mainly used to run tasks at specified time or periodically in Java threads. It is thread-safe, but does not provide real-time guarantees.

  

  The concept of daemon threads was mentioned above.

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

  The so-called daemon thread refers to a thread that provides a general service in the background when the program is running. For example, the garbage collection thread is a very competent guardian, and this thread is not an indispensable part of the program. Therefore, when all non-daemon threads end, the program terminates, killing all 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 a daemon thread and a user thread is the departure of the virtual machine: if all the user threads have exited, only the daemon thread exists, and the virtual machine will exit. Because there is no guardian, the daemon thread has no work to do, and there is no need to continue running the program.

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

  (1) thread.setDaemon(true) must be set before thread.start(), otherwise an IllegalThreadStateException will be thrown. You cannot set a running regular thread as a daemon thread.

  (2) The new thread generated in the Daemon thread is also Daemon's.

  (3) A daemon thread should never access inherent resources, such as files, databases, because it will be interrupted at any time even in the middle of an operation.

  

  Let's write a case next, so that after the program runs for 3 seconds, it prints "time to get up" 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);
        //Execute the task after a delay of 3 seconds
        timer.schedule(new MyTask(),3000);//The unit is milliseconds
    }
}
class MyTask extends TimerTask{
    @Override
    public void run() {
        System.out.println("It's time to get up");
        String now2 = TimerTest.dateFormat.format(System.currentTimeMillis());
        System.out.println(now2);
    }
}

   After the above code is executed, it prints "time to get up" after a delay of 3 seconds, as shown below:

21:26:18
Time to wake up
21:26:21

   Other methods of Timer:

  schedule(TimerTask task, Date time) Execute the TimerTask task once on the specified date; if the date time is earlier than the current time, execute it immediately.

  schedule(TimerTask task, long delay, long period) takes the current time as the benchmark, delays the specified milliseconds, and then executes the TimerTask task infinitely at the specified time interval.

  schedule(TimerTask task, Date firstTime, long period) Execute the TimerTask task an infinite number of times at the specified time interval after the specified date.

  scheduleAtFixedRate(TimerTask task, long delay, long period) Based on the current time, after a delay of the specified milliseconds, periodically execute the TimerTask task for an infinite number of times at the specified time interval.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837889&siteId=291194637