Test Purpose
Learn the specific case of Newscheduledthreadpool related usage created by the Scheduledexecutorservice class
Create the use of Newscheduledthreadpool and Scheduleatfixedrate and Schedulewithfixeddelay methods.
Package com.secbro.test.thread;
Import Java.text.DateFormat;
Import Java.util.Date;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;
Import Java.util.concurrent.TimeUnit; /** * Create a thread pool of infinite size.
This thread pool supports the need for timing and periodic execution of tasks.
* @author Zhuzhisheng * @Description * @date on 2016/6/1. */public class Testnewscheduledthreadpool {public static void main (string[] args) {Scheduledexecutorservi
CE service = executors.newscheduledthreadpool (2);
Scheduleatfixedrate (service,1000);
Scheduleatfixedrate (service,6000);
Schedulewithfixeddelay (service,1000);
Schedulewithfixeddelay (service,6000); private static void Scheduleatfixedrate (Scheduledexecutorservice service, final int sleeptime) {Service.sch Eduleatfixedrate (New Runnable () {@Override public void run () {Long start = new D
Ate (). GetTime (); System.out.println ("Scheduleatfixedrate starts executingTime: "+ dateformat.gettimeinstance (). Format (new Date ());
try {thread.sleep (sleeptime);
catch (Interruptedexception e) {e.printstacktrace ();
Long end = new Date (). GetTime ();
System.out.println ("scheduleatfixedrate execution takes time =" + (End-start)/1000 + "M"); System.out.println ("Scheduleatfixedrate Execution Time:" + dateformat.gettimeinstance (). Format (new Date ())
);
System.out.println ("======================================");
}},1000,5000,timeunit.milliseconds); } private static void Schedulewithfixeddelay (scheduledexecutorservice service,final int sleeptime) {SERVICE.S Chedulewithfixeddelay (New Runnable () {@Override public void run () {Long start =
New Date (). GetTime (); System.out.println ("schedulewithfixedDelay Start Execution Time: "+ dateformat.gettimeinstance (). Format (new Date ());
try {thread.sleep (sleeptime);
catch (Interruptedexception e) {e.printstacktrace ();
Long end = new Date (). GetTime ();
System.out.println ("Schedulewithfixeddelay execution takes time =" + (End-start)/1000 + "M"); System.out.println ("Schedulewithfixeddelay Execution Time:" + dateformat.gettimeinstance (). Format (New Date (
)));
System.out.println ("======================================");
}},1000,5000,timeunit.milliseconds);
}
}Specific Analysis
To create a Scheduledexecutorservice thread pool method, the following is to create a thread pool of size 2:
Scheduledexecutorservice service = Executors.newscheduledthreadpool (2);
scheduleatfixedrate Experiment One
Scheduleatfixedrate (service,1000);
The output results are:
Scheduleatfixedrate Start Execution time: 15:03:15
scheduleatfixedrate Execution time =1m
scheduleatfixedrate execution Time: 15:03:16
======================================
scheduleatfixedrate start execution time: 15:03:20
scheduleatfixedrate Execution takes time =1m
scheduleatfixedrate execution time: 15:03:21
======================================
Analysis: In the case that the task execution time is less than the interval time, the program takes the starting time as the guideline, executes once at a specified time, and is not affected by the task execution time. Experiment Two
Scheduleatfixedrate (service,6000);
The output results are:
Scheduleatfixedrate Start Execution time: 15:06:12
scheduleatfixedrate Execution time =6m
scheduleatfixedrate execution Time: 15:06:18
======================================
scheduleatfixedrate start execution time: 15:06:18
scheduleatfixedrate Execution takes time =6m
scheduleatfixedrate execution time: 15:06:24
======================================
Scheduleatfixedrate Start Execution time: 15:06:24
scheduleatfixedrate Execution time =6m
scheduleatfixedrate execution Time: 15:06:30
The analysis concludes that when the task time is greater than the interval, this method does not reopen a new task to execute, but waits for the original task to complete, and immediately opens the next task for execution. At this time, the execution interval has been disrupted. Schedulewithfixeddelay Experiment One
Schedulewithfixeddelay (service,1000);
The output results are:
Schedulewithfixeddelay Start Execution time: 15:11:03
schedulewithfixeddelay Execution time =1m
schedulewithfixeddelay Execution Time: 15 : 11:04
======================================
schedulewithfixeddelay start execution time: 15:11:09
Schedulewithfixeddelay execution takes time =1m
schedulewithfixeddelay execution time: 15:11:10
================================ ======
The analysis concludes that when the task is less than the delay, the first task executes, delays the specified time, and then begins the second task. Experiment Two
Schedulewithfixeddelay (service,6000);
The output results are:
Schedulewithfixeddelay Start Execution time: 15:12:53
schedulewithfixeddelay Execution time =6m
schedulewithfixeddelay Execution Time: 15 : 12:59
======================================
schedulewithfixeddelay start execution time: 15:13:04
Schedulewithfixeddelay execution takes time =6m
schedulewithfixeddelay execution time: 15:13:10
============================== ========
Concludes that when the execution of a task is greater than the delay time, the first task executes, delays the specified time, and then begins the second task.
In summary: This method, regardless of the duration of the task's execution, delays the specified time and then begins the second task, after the first task completes.