Java multithreading 03: daemon & Timer
Posted by BDKR on Thu, 09 Dec 2021 06:18:12 +0100
1, Daemon thread
- Threads in the Java language fall into two categories:
- User thread
- Daemon thread (background thread)
- Features of guard thread: generally, guard thread is an endless loop. As long as all user threads end, the guard thread ends automatically.
——The representative one is garbage collection thread (daemon thread).
Note: the main method of the main thread is a user thread - Where are daemon threads used?
The system data is automatically backed up at 00:00 every day. This requires a timer, and we can set the timer as a daemon thread. Always watch there and back up every 00:00. If all user threads end, the daemon thread will exit automatically. There is no need to back up the data. - Void setdaemon (Boolean on): marks the thread as a daemon or user thread.
/*
Daemon thread
*/
public class ThreadTest14 {
public static void main(String[] args) {
Thread t = new BakDataThread();
t.setName("Thread backing up data");
// Set the thread as a daemon before starting the thread
t.setDaemon(true);
t.start();
// Main thread: the main thread is the user thread
for(int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class BakDataThread extends Thread {
public void run(){
int i = 0;
// Even if it is an endless loop, because the thread is the guardian, when the user thread ends, the guardian thread automatically terminates.
while(true){
System.out.println(Thread.currentThread().getName() + "--->" + (++i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2, Timer
- Function of timer: execute specific programs at specific intervals.
Example:
General ledger operation of bank account shall be carried out every week.
Backup data every day.
- In actual development, it is very common to execute a specific program every few days.
- In fact, it can be implemented in many ways in Java:
- You can use the sleep method.
Set the sleep time, wake up at this time point and perform tasks. This method is the most primitive timer. - A timer has been written in the java class library: java.util.Timer, which can be used directly.
This method is rarely used in current development, because there are many high-level frameworks that support timed tasks. - In the actual development, the SpringTask framework provided in the Spring framework is widely used at present.
This framework can complete the task of timer as long as it is simply configured.
/*
Use a timer to specify a scheduled task.
*/
public class TimerTest {
public static void main(String[] args) throws Exception {
// Create timer object
Timer timer = new Timer();
//Timer timer = new Timer(true); // Guard thread mode
// Specify scheduled tasks
//Timer.schedule (scheduled task, first execution time, and how often to execute it);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstTime = sdf.parse("2020-03-14 09:34:30");
//timer.schedule(new LogTimerTask() , firstTime, 1000 * 10);
// Once a year.
//timer.schedule(new LogTimerTask() , firstTime, 1000 * 60 * 60 * 24 * 365);
//Anonymous inner class method
timer.schedule(new TimerTask(){
@Override
public void run() {
// code....
}
} , firstTime, 1000 * 10);
}
}
// Write a scheduled task class
// Suppose this is a scheduled task for logging
class LogTimerTask extends TimerTask {
@Override
public void run() {
// Just write the tasks you need to perform.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTime = sdf.format(new Date());
System.out.println(strTime + ":Successfully completed a data backup!");
}
}
Topics:
Java
Back-end