1. The Timer and TimerTask
- Timer: Sets a scheduled task
- TimerTask: Implementation of a task
2. Schedule (time task, Date time) method
Execute a scheduled task on a specified date
2.1 Perform tasks later than the current time
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer();
timer.schedule(getTimerTask(), planDate);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task run date=" + newDate()); }}; }}Copy the code
2.2 If a task is executed earlier than the current time, the task is executed immediately
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 10);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("task planDate=" + planDate);
Timer timer = new Timer();
timer.schedule(getTimerTask(), planDate);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task run date=" + newDate()); }}; }}Copy the code
2.3 Executing Multiple Tasks
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
Date planDate1 = calendar.getTime();
calendar.add(Calendar.SECOND, 5);
Date planDate2 = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("task1 planDate1=" + planDate1);
System.out.println("task2 planDate2=" + planDate2);
Timer timer = new Timer();
timer.schedule(getTimerTask1(), planDate1);
timer.schedule(getTimerTask2(), planDate2);
System.out.println("main end");
}
private static TimerTask getTimerTask1(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task1 run date=" + newDate()); }}; }private static TimerTask getTimerTask2(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task2 run date=" + newDate()); }}; }}Copy the code
2.4 Delayed execution of multiple tasks
TimerTask tasks are executed sequentially, one by one, in a queue, so the actual execution time of the tasks may be inconsistent with the planned execution time, because the previous tasks may take too long to execute, resulting in the execution of later tasks being delayed
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
Date planDate1 = calendar.getTime();
calendar.add(Calendar.SECOND, 5);
Date planDate2 = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("task1 planDate1=" + planDate1);
System.out.println("task2 planDate2=" + planDate2);
Timer timer = new Timer();
timer.schedule(getTimerTask1(), planDate1);
timer.schedule(getTimerTask2(), planDate2);
System.out.println("main end");
}
private static TimerTask getTimerTask1(a) {
return new TimerTask() {
@Override
public void run(a) {
try {
System.out.println("task1 run begin date=" + new Date());
Thread.sleep(20000);
System.out.println("task1 run end date=" + new Date());
} catch(InterruptedException e) { e.printStackTrace(); }}}; }private static TimerTask getTimerTask2(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task2 run begin date=" + new Date());
System.out.println("task2 run end date=" + newDate()); }}; }}Copy the code
2.5 Process Not Destroyed?
-
As you can see, the process is not destroyed after the automatic task is completed, because creating a Timer is to start a new thread. This thread is not a daemon thread by default, so it is always running
-
The Timer can be set to run as a daemon thread
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer(true);
timer.schedule(getTimerTask(), planDate);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task run Date=" + newDate()); }}; }}Copy the code
After the above code is executed, the process is destroyed after printing “main End “and the TimerTask is not executed. Because the main thread is destroyed after execution, the Timer is destroyed as a daemon thread
3. Schedule (TimerTask task, Date firstTime, long period
After a specified date is specified, the task is executed indefinitely at a specified interval
3.1 Executing a task later than the current time
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 5);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer();
timer.schedule(getTimerTask(), planDate, 3000);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task run date=" + newDate()); }}; }}Copy the code
3.2 If a task is executed earlier than the current time, the task is executed immediately
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 10);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer();
timer.schedule(getTimerTask(), planDate, 3000);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
System.out.println("task run date=" + newDate()); }}; }}Copy the code
3.3 Delayed Execution
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 5);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer();
timer.schedule(getTimerTask(), planDate, 3000);
System.out.println("main end");
}
private static TimerTask getTimerTask(a) {
return new TimerTask() {
@Override
public void run(a) {
try {
System.out.println("task run begin date=" + new Date());
Thread.sleep(4000);
System.out.println("task run end date=" + new Date());
} catch(InterruptedException e) { e.printStackTrace(); }}}; }}Copy the code
4. The cancel () method
4.1 TimerTask’s cancel() method
The TimerTask class’s cancel() method removes itself from the task list
public class Test {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("currentDate=" + currentDate);
Timer timer = new Timer();
timer.schedule(new TimerTaskA(), currentDate, 2000);
timer.schedule(new TimerTaskB(), currentDate, 3000);
System.out.println("main end");
}
static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run date=" + new Date());
this.cancel(); }}static class TimerTaskB extends TimerTask {
@Override
public void run(a) {
System.out.println("taskB run date=" + newDate()); }}}Copy the code
4.2 Cancel () method of the Timer class
The cancel() method of the Timer class clears all tasks from the task queue
public class Test {
public static void main(String[] args) throws InterruptedException {
Date currentDate = new Date();
System.out.println("currentDate=" + currentDate);
Timer timer = new Timer();
timer.schedule(new TimerTaskA(), currentDate, 2000);
timer.schedule(new TimerTaskB(), currentDate, 3000);
Thread.sleep(10000);
timer.cancel();
System.out.println("main end");
}
static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run date=" + newDate()); }}static class TimerTaskB extends TimerTask {
@Override
public void run(a) {
System.out.println("taskB run date=" + newDate()); }}}Copy the code
4.3 Precautions for the Cancel () method of the Timer class
The Cancel () method of the Timer class sometimes does not necessarily stop the task because the Cancel () method of the Timer class sometimes does not grab the queue lock
public class Test {
private static int i = 1;
public static void main(String[] args) {
while (true) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 3);
Timer timer = new Timer();
timer.schedule(newTimerTaskA(), calendar.getTime()); timer.cancel(); i++; }}static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run i=" + i);
this.cancel(); }}}Copy the code
5. Schedule (TimerTask, long delay) method
Based on the time when the method is executed as the reference time, the TimerTask is executed after the specified number of milliseconds
public class Test {
public static void main(String[] args) {
System.out.println("currentTime=" + new Date());
Timer timer = new Timer();
timer.schedule(new TimerTaskA(), 2000);
System.out.println("main end");
}
static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run date=" + newDate()); }}}Copy the code
6. Schedule (TimerTask task, long delay, long period) method
The time when the method is executed is used as the reference time, and then the task is executed in an infinite cycle at an interval of a specified number of milliseconds
public class Test {
public static void main(String[] args) {
System.out.println("currentTime=" + new Date());
Timer timer = new Timer();
timer.schedule(new TimerTaskA(), 2000.5000);
System.out.println("main end");
}
static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run date=" + newDate()); }}}Copy the code
7. The catchability of scheduleAtFixedRate() method
The scheduleAtFixedRate() method is catch-up. If the task is executed earlier than the current time, calling this method will “supplement” the execution of the corresponding task in the earlier time period
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 20);
Date planDate = calendar.getTime();
System.out.println("currentDate=" + new Date());
System.out.println("planDate=" + planDate);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTaskA(), planDate, 2000);
System.out.println("main end");
}
static class TimerTaskA extends TimerTask {
@Override
public void run(a) {
System.out.println("taskA run date=" + newDate()); }}}Copy the code
Learn from “Java Multi-threaded Programming Core Technology”