1.1.
thread pool for timers
When the thread pool is used for timer scheduling, you can only specify a time period after the task is executed, but you cannot specify a specific point in time.
1.2.
timers that run only once
5 seconds after the timer for the specified task is run, the timer task will only be executed 1 times.
Scheduledexecutorservice executorservice = Executors.newscheduledthreadpool (3new Runnable () {@Overridepublicvoid run () {System.out.println (new Date () + " "+1000 * 5
1.3.
fixed-Rate timers
A fixed-rate timer runs the task for the first time after a specified period and repeats the task after every fixed period of time thereafter. The following code creates a timer that runs for the first time after 5 seconds and repeats 1 times every 1 seconds .
Scheduledexecutorservice executorservice = Executors.newscheduledthreadpool (3new Runnable () {@Overridepublicvoid run () {System.out.println (New Date () + " " ++-5, 1000*1, timeunit.milliseconds);
Scheduled:sun Mar 19:55:07 CST 2017 8
Scheduled:sun Mar 19:55:08 CST 2017 8
Scheduled:sun Mar 19:55:09 CST 2017 10
Scheduled:sun Mar 19:55:10 CST 2017 10
Scheduled:sun Mar 19:55:11 CST 2017 10
Scheduled:sun Mar 19:55:12 CST 2017 10
Scheduled:sun Mar 19:55:13 CST 2017 11
Scheduleatfixedrate() the API doc for this function is described in this way:
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; That is executions would commence after initialdelay then Initialdelay+period, then InitialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions is suppressed. Otherwise, the task would only terminate via cancellation or termination of the executor. If any execution of this task takes longer than it period, then subsequent executions could start late, but would not concur rently Execute.
Parameters:
command the task to execute
InitialDelay The time to delay first execution
period the period between successive executions
Unit The time unit of the InitialDelay and period parameters
Returns:
A scheduledfuture representing pending completion of the task, and whose get () method would throw an exception upon Cancell ation
The translation may not be very accurate, the general meaning is:
creates and executes a periodic action, which is enabled for the first time after a given initial delay and continues execution at a given time interval. The execution of this action takes place for the first time after initialdelay units, then initialdelay+period,InitialDelay + Period and so on. If an exception occurs for any one execution of a scheduled task, subsequent executions will be stopped. If no exception occurs, the scheduled task terminates until the thread pool executor terminates or cancels. If any one execution of the scheduled task takes longer than its time interval (period), subsequent execution will delay the start rather than executing in parallel.
Parameters :
Command: the task to be executed.
InitialDelay: initial time delay.
Period: The time interval between adjacent executions.
Unit: time units.
return value:
a a Scheduledfuture object that is used to express tasks that have not yet been completed. When the task is canceled , its get) method throws an exception.
Task Delay Scenario:
Scheduledexecutorservice executorservice = Executors.newscheduledthreadpool (3new Runnable () {@Overridepublicvoid run () {System.out.println (new Date () + " "+ Thread.CurrentThread (). GetId ()); Try {thread.sleep (1000*3catch5, 1000*1, timeunit.milliseconds);
Sleep () causes the task to delay, at which time the task executes 1 times per second and becomes 1 times per 3 seconds .
Scheduled:sun Mar 20:55:34 CST 2017 8
Scheduled:sun Mar 20:55:37 CST 2017 8
Scheduled:sun Mar 20:55:40 CST 2017 10
Scheduled:sun Mar 20:55:43 CST 2017 10
Scheduled:sun Mar 20:55:46 CST 2017 10
Scheduled:sun Mar 20:55:49 CST 2017 10
Scheduled:sun Mar 20:55:52 CST 2017 10
Scheduled:sun Mar 20:55:55 CST 2017 10
1.4.
fixed-Delay timers
Scheduledexecutorservice executorservice = Executors.newscheduledthreadpool (3new Runnable () {@Overridepublicvoid run () {System.out.println (new Date () + " "+1000 * 5, 1000*1
Scheduled:sun Mar 20:51:29 CST 2017 8
Scheduled:sun Mar 20:51:30 CST 2017 8
Scheduled:sun Mar 20:51:31 CST 2017 10
Scheduled:sun Mar 20:51:32 CST 2017 10
Scheduled:sun Mar 20:51:33 CST 2017 10
Scheduled:sun Mar 20:51:34 CST 2017 10
Scheduled:sun Mar 20:51:35 CST 2017 10
Scheduled:sun Mar 20:51:36 CST 2017 10
Scheduled:sun Mar 20:51:37 CST 2017 11
Scheduled:sun Mar 20:51:38 CST 2017 11
The situation where the task is delayed:
Scheduledexecutorservice executorservice = Executors.newscheduledthreadpool (3new Runnable () {@Overridepublicvoid run () {System.out.println (new Date () + " "+ Thread.CurrentThread (). GetId ()); Try {thread.sleep (1000*3catch1000 * 5, 1000*1
The original plan was to perform 1 missions per second , turning 1 times every 4 seconds . The subsequent delay period is different from the fixed ratio timer. The fixed rate becomes 3 seconds, and the fixed delay becomes 4 seconds.
The fixed ratio can be understood as the execution time after the end of the previous task has elapsed beyond the delay interval and is not waiting but immediately begins the follow-up task. A fixed delay can be understood as the delay in the normal calculation of subsequent tasks after the previous task has been completed.
Scheduled:sun Mar 20:35:44 CST 2017 8
Scheduled:sun Mar 20:35:48 CST 2017 8
Scheduled:sun Mar 20:35:52 CST 2017 10
Scheduled:sun Mar 20:35:56 CST 2017 10
Scheduled:sun Mar 20:36:00 CST 2017 10
Scheduled:sun Mar 20:36:04 CST 2017 10
Scheduled:sun Mar 20:36:08 CST 2017 10
Scheduled:sun Mar 20:36:12 CST 2017 10
Scheduled:sun Mar 20:36:16 CST 2017 10
Scheduled:sun Mar 20:36:20 CST 2017 10
Scheduled:sun Mar 20:36:24 CST 2017 10
Scheduled:sun Mar 20:36:28 CST 2017 10
Scheduled:sun Mar 20:36:32 CST 2017 10
Scheduled:sun Mar 20:36:36 CST 2017 10
Java Concurrency Programming (8) thread pool for timers