The thread pool class is java.util.concurrent.ThreadPoolExecutor, and the common construction method is:
Threadpoolexecutor (intcorepoolsize, int maximumpoolsize,
Longkeepalivetime, Timeunit unit, Blockingqueueworkqueue,
Rejectedexecutionhandlerhandler)
Corepoolsize: Thread pool maintains minimum number of threads
Maximumpoolsize: Thread pool maintains maximum number of threads
KeepAliveTime: Thread pool maintains idle time allowed by threads
Unit: The units in which the thread pool maintains the idle time allowed by threads
Workqueue: Buffer queues used by the thread pool
Handler: thread pool processing policy for reject tasks
A task is added to the thread pool through the Execute (Runnable) method, and the task is a Runnable type object, and the task is performed by the Runnable type object's Run () method.
When a task is added to the thread pool through the Execute (Runnable) method:
1. If the number of threads in the thread pool is less than corepoolsize, create a new thread to handle the added task even if the threads in the thread pool are idle.
2, if the number of threads in the pool equals corepoolsize, but the buffer queue Workqueue is not full, then the task is put into the buffer queue.
3, if the number of threads in the pool is greater than corepoolsize, the buffer queue is workqueue full, and the number of thread pools is less than maximumpoolsize, a new thread is built to handle the added task.
4, if the number of threads in the pool is greater than corepoolsize, the buffer queue is workqueue full, and the number in the thread pool equals maximumpoolsize, the task is handled by handler the policy specified.
The priority of the processing task is:
Core thread corepoolsize, Task queue workqueue, maximum thread maximumpoolsize, if all three are full, use handler to process the rejected task.
When the number of threads in the thread pool is greater than corepoolsize, the thread is terminated if a thread is idle longer than KeepAliveTime. This allows the thread pool to dynamically adjust the number of threads in the pool.
The optional parameters of the unit are several static properties in Java.util.concurrent.TimeUnit:
nanoseconds
microseconds
Milliseconds
SECONDS
Workqueue I used to be: java.util.concurrent.ArrayBlockingQueue
Handler has four options:
1, Threadpoolexecutor.abortpolicy ()
Throw Java.util.concurrent.RejectedExecutionException exception
2, Threadpoolexecutor.callerrunspolicy ()
Retry adding the current task, he will automatically repeatedly invoke the Execute () method
3, Threadpoolexecutor.discardoldestpolicy ()
Throw away the old task
4, Threadpoolexecutor.discardpolicy ()
Discard the current task
Example:
Import Java.util.concurrent.ArrayBlockingQueue;
Import Java.util.concurrent.ThreadPoolExecutor;
Import Java.util.concurrent.TimeUnit;
public class Testthreadpool {private static int producetasksleeptime = 2;
private static int producetaskmaxnumber = 10; public static void Main (string[] args) {//construct a thread pool threadpoolexecutor threadPool = new Threadpoolexecutor (2, 4, 3
, Timeunit.seconds, New arrayblockingqueue<runnable> (3), New Threadpoolexecutor.discardoldestpolicy ()); for (int i = 1; I <= producetaskmaxnumber i++) {try {//produce a task and add it to the thread pool String
Task = "task@" + i;
System.out.println ("put" + Task);
Threadpool.execute (New Threadpooltask (Task));
Easy to observe, wait for some time thread.sleep (producetasksleeptime);
catch (Exception e) {e.printstacktrace ();
The task * */public class Threadpooltask implements Runnable, Serializable {/** * thread pool * Private static final Long serialversionuid = 0;
The data required to save the task is private Object threadpooltaskdata;
Threadpooltask (Object tasks) {this.threadpooltaskdata = tasks;}
private static int consumetasksleeptime = 2000;
The public void Run () {//Process a task, where the processing is too simple, just a print statement System.out.println ("Start ..." + threadpooltaskdata);
try {////easy to observe, wait for a period of time thread.sleep (Consumetasksleeptime);
catch (Exception e) {e.printstacktrace ();
} threadpooltaskdata = null;
Public Object Gettask () {return this.threadpooltaskdata;}
}
Description
1, in this program, a task is a runnable type of object, that is, a threadpooltask type of object.
2, in general, the task in addition to processing methods, but also need to deal with the data, processing data through the construction method to the task.
3. In this program, the main () method is equivalent to a brutal leader, who sends out many tasks and throws it to a hard-working team called ThreadPool.
There are at least two players in the team, and if the two of them are busy, the task is placed in the task list.
If the backlog of tasks is too much to fit into the list of tasks (more than 3), hire new players to help. But based on cost considerations, it is not possible to hire too many players and hire at most 4.
If all four players are busy and have new tasks, the team will not be able to handle the task, and the tasks would be dealt with by a strategy, and our approach is to continue to be distributed until the task is accepted (more brutal.) OH).
Because the team work is the cost, if the work is very idle, idle to 3 seconds have no new task, then some players will be dismissed, but for the normal operation of the group, even if the work again idle, team members can not be less than two.
4, by adjusting the size of producetasksleeptime and consumetasksleeptime to achieve the distribution of tasks and the speed of processing tasks control, change these two values can be observed at different rates of the work of the program.
5, through the adjustment of the data referred to in 4, plus the adjustment task discard strategy, put on the other three strategies, you can see the different strategies under different processing methods.
6, for other uses, see the help of the JDK, it is easy to understand and use.
Mass video capture Vue Angular spring