Workerthread class
package com.cninfo.threadtest;import java.util.date;public class workerthread implements runnable{ private string taskname; public Workerthread (int i) { this.taskname= "+i+" Tasks "; } @Override public void run () { system.out.println (Thread.CurrentThread (). GetName () + " at "+new date () + " do "+this.taskname); processcommand (); system.out.println ( Thread.CurrentThread (). GetName () + " at " +new date () + finished +this.taskname); } private void processcommand () { try {&nbSp; thread.sleep (; ) } catch (interruptedexception e) { e.printstacktrace (); } } @Override public String tostring () { return this.taskname; }}
Scheduledthreadpool class
package com.cninfo.threadtest;import java.util.date;import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice;import java.util.concurrent.timeunit; Public class scheduledthreadpool { public static void main (String[] args) throws InterruptedException { The //thread pool initializes only 2 threads to perform the task Scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool (2); //schedule to run after sometime system.out.println ("current time = " +new Date ()); for (int i=1; i<3; i++) { //thread. Sleep ( workerthread worker); = new workerthread (i); Threads in the thread pool delay 10 seconds to execute a task that is dropped into the thread pool, and the task in the thread pool executes the first time without executing the //scheduledthreadpool.schedule (worker, 10, timeunit.seconds); ///threads in a thread pool, delaying a 10-second periodic execution of a task thrown into the thread pool, when a thread in the thread pool executes all the tasks in the pool, relative to the fixed period of time for the first execution (if it is executed immediately), the thread begins to re-execute the task in the thread pool // Scheduledthreadpool.scheduleatfixedrate (worker, 10, 60,timeunit.seconds); //scheduledthreadpool.scheduleatfixedrate (WORKER,&NBSP;10, 3,timeunit.seconds); // Threads in the thread pool, deferred for 10 seconds, are periodically thrown into a task in the thread pool, and when a thread in the thread pool executes all of the tasks in the pool, the thread begins to re-execute the task in the thread pool relative to the time interval fixed at this point //scheduledthreadpool.schedulewithfixeddelay (worker, 10, 60, timeunit.seconds); Scheduledthreadpool.schedulewithfixeddelay (worker, 10, 3, timeunit.seconds); } system.out.println ("All tasks are thrown into the thread pool"); //add some delay to let some threads spawn by scheduler //tHread.sleep (30000); //scheduledthreadpool.shutdown (); /* while (!scheduledthreadpool.isterminated ()) { //wait for all tasks to finish }*/ System.out.println ("Finished all threads"); } }
Schedule
Creates and executes a one-time operation that is enabled after a given delay.
Schedulewithfixeddelay:
The official meaning is: Create an interval task with a given initial delay, and the next execution time is the time required for the last task from execution to completion + the given interval. For example, for example, the initial delay for a given task (long InitialDelay) is 12:00 and the interval is 1 minutes. Then this task will be created and executed at 12:00 for the first time, and if the task takes 1 minutes from execution to completion, then the next task execution time should be 12:01 plus the set interval of 1 minutes, then the next task is 12:02. The interval time (delay) is calculated from the end of the last task execution. if the execution takes longer than its cycle, the next task waits until after the task has ended, waiting for the interval (delay) before executing
Scheduleatfixedrate:
The Official document means: Create an interval task with a given initial delay, after which each task execution time is the initial delay + N * Delay (interval). Here n is the nth task after the first task executes, n starts at 1, and consciousness is the first time the task is executed at 12:00 then the next task execution time is fixed 12:01 next to 12:02. The biggest difference from Schedulewithfixeddelay is that scheduleatfixedrate is not affected by task execution time. The interval here refers to a fixed interval of time, and if any one of the tasks encounters an exception, subsequent executions will be canceled. Otherwise, the task can only be terminated by executing the cancellation or termination method of the program. if any one execution of this task takes longer than its period, subsequent executions will be deferred, but will not be executed at the same time. If the execution takes longer than its cycle, the next task, etc., is executed immediately after the task is completed.
In general, the number of threads in a thread pool is not as good as it is to be set according to the performance of the machine, and second, the number of tasks placed in the thread pool is preferably less than the number of threads in the thread pool.
This article from "Small Fish Blog" blog, declined reprint!
Java scheduledthreadpoolexecutor delay or periodic execution of tasks