The previous two posts, "Task Scheduler (i)--JDK timer" and "task scheduling (ii)--JDK the timer Dynamic Modification task Execution plan", briefly introduced a timer, you can achieve a few of the functions, but in the multithreading aspect is slightly insufficient.
According to the timer source code, you can see that the timer is single-threaded. So the task is serially executed. If one of the task executions takes a long time, the rest of the task can only be dry. What to do!
This is the kind of problem that is now being encountered. Not because of this small problem to change other task scheduling framework, or want to use the simplest solution. So Scheduledexecutorservice was chosen by me. How did you find this? 1. Online search, 2. Take a good look at the Timer class comments:
Translation: java5.0 introduced the java.util.concurrent package, where Java.util.concurrent.scheduledthreadpoolexecutor is one of the concurrency utilities. Scheduledthreadpoolexecutor is a thread pool that can perform tasks repeatedly, and you can specify the interval and delay time for tasks. It is a more generic alternative than timer/timertask. Because it allows multiple service threads to accept different time units, and does not need to inherit timetask (only need to implement the Runnable interface). The configuration Scheduledthreadpoolexecutor is single-threaded, and is equivalent to using a timer.
Scheduledthreadpoolexecutor implements the Scheduledexecutorservice interface, so the name of the interface is used in the header.
The Scheduledexecutorservice provides 4 methods:
The second method is a bit more special, the first parameter is callable, the other is runnable, the difference between the two is no longer within the scope of this blog post. Skip this. Say some of the other three methods.
Schedule () method The first parameter is a task instance, the second parameter is the delay time, and the third is the time unit. For example, call the following:
Scheduledexecutorservice pool = Executors.newscheduledthreadpool (2);p ool.schedule (Task1, 5, timeunit.seconds);// After 5s delay, execute and execute only once Task1
The Scheduleatfixedrate () and Schedulewithfixeddelay method parameters are the same. The first parameter is the task instance, the second parameter is the delay time, the third is the interval time, and the fourth is the time unit. The difference between the two methods can be seen in the method name: The Scheduleatfixedrate method performs the task at a fixed frequency. The Schedulewithfixeddelay method is to perform the task according to a fixed delay.
/** * Task1 * * @author Arron * @date August 5, 2015 PM 2:08:34 * @version 1.0 */public class Task1 implements R unnable{@SuppressWarnings ("deprecation") public void Run () {System.out.println ("----task1 start--------" +new Date (). toLocaleString ()); try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN ("----3s later, Task1 end--------" +new Date (). toLocaleString ());}}
To Test the Scheduleatfixedrate method:
public static void Main (string[] args) {Scheduledexecutorservice pool = executors.newscheduledthreadpool (1); Task1 T1 = new Task1 ();//immediately after the completion of the T1,3S task, and then wait for 2s (interval-Elapsed time), if there is a free thread, perform the task again pool.scheduleatfixedrate (T1, 0, 5, Timeunit.seconds);}
Execution Results
The second implementation of TASK1 is that there are currently free threads.
The start time of execution is the last end time + (interval-time that the task is consumed). Adding this difference is less than 0, that is, the interval is less than the time the task consumes, then it will not wait, will execute immediately (of course, to meet the premise).
To test the Scheduleatfixedrate method:
public static void Main (string[] args) {Scheduledexecutorservice pool = executors.newscheduledthreadpool (1); Task1 T1 = new Task1 ();//immediately after the completion of the T1,3S task, and then wait for 2s (interval-Elapsed time), if there is a free thread, perform the task again Pool.schedulewithfixeddelay (T1, 0, 5, Timeunit.seconds);}
Execution Results
Simply introduce here, the next one will share the code to replace the timer.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Task scheduling (iii)--timer Alternatives Scheduledexecutorservice Introduction