Overview:
The thread pool mechanism is to create some threads in advance to wait for a call from the service-side program, which is stored in an array structure called a "thread pool." When the server has a task to execute, it pulls a thread out of the thread pool and assigns it a task, and when the thread task executes, it is put back into the thread pool.
Advantages:
1. Because the thread already exists before the task arrives, the system eliminates the overhead of thread-created resources and time. You can service the request immediately, making the program respond faster.
2. By properly adjusting the number of threads in the thread pool, forcing some new tasks to wait will prevent the resources from running out.
Reference:
Http://www.cnblogs.com/dolphin0520/p/3932921.html
"Java language Programming"--Tsinghua University Press
Examples and Analysis:
Mytask.java
public class MyTask implements Runnable {private int tasknum;public mytask (int num) {this.tasknum = num;} @Overridepublic void Run () {System.out.println ("executing task" + tasknum), try {thread.currentthread (). Sleep (3000);} catch (Interruptedexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("task" + Tasknum + "execution complete");}}
The procedure above is simple, just to print some simple information.
Executor.java
public class Executor {public static void main (string[] args) {threadpoolexecutor Executor = new Threadpoolexecutor (5, 10, Timeunit.milliseconds, New Arrayblockingqueue<runnable> (5)); for (int i = 0; i <; i++) {MyTask MyTask = n EW MyTask (i), if (!executor.isshutdown ()) {Executor.execute (mytask);} Printexecutor (executor);} Executor.shutdown ();} private static void Printexecutor (Threadpoolexecutor executor) {System.out.println ("thread pool number of threads:" + executor.getpoolsize () + ", Number of tasks waiting to be performed in the queue:" + executor.getqueue (). Size () + ", Number of tasks performed:" + Executor.getcompletedtaskcount ());}}
It uses the class Threadpoolexecutor that Java provides for us, which is a good class. The constructor used to create the instance is as follows:
Public threadpoolexecutor (int corepoolsize, int maximumpoolsize, long KeepAliveTime, timeunit Unit, blockingqueue<runnable> WorkQueue) {This (corepoolsize, maximumpoolsize, KeepAliveTime, Unit, WorkQueue, executors.defaultthreadfactory (), DefaultHandler); }
It contains one of the following parameters:
int corepoolsize: Thread pool threads
int maximumpoolsize: The maximum number of threads in a thread pool
Long KeepAliveTime: Indicates the maximum length of time a thread will be terminated without a task executing
Timeunit Unit: The time unit of the parameter KeepAliveTime, which has 7 static properties in the Timeunit class:
Timeunit.days; Days Timeunit.hours; Hour timeunit.minutes; Minutes Timeunit.seconds; Seconds timeunit.milliseconds; Ms Timeunit.microseconds; Subtle timeunit.nanoseconds; Na-Sec
Blockingqueue<runnable> WorkQueue: A blocking queue that stores tasks waiting to be executed
<pre name= "code" class= "Java" >ArrayBlockingQueue; Linkedblockingqueue; Synchronousqueue;
The Arrayblockingqueue blocking queue is used in the above code, and parameter 5 is passed in. One thing to note here is that if we assume that all tasks are mtaskcount (that is, 15 in the code), then we'd better meet the formula:maximumpoolsize >= mtaskcount-corepoolsize , or a Java.util.concurrent.RejectedExecutionException exception will be thrown. Of course, you can also have a common behavior: set Maximumpoolsize to Integer.max_value.
There is also a way to remedy:
Threadpoolexecutor executor = new Threadpoolexecutor (5, 5, $, timeunit.milliseconds, new linkedblockingqueue< Runnable> ());
But perhaps the use of threadpoolexecutor may be less frequent at the moment. Because Java encapsulates some of the more convenient ways to create it:
Executorservice Executor1 = Executors.newfixedthreadpool (5); Create a fixed capacity buffer pool Executorservice Executor2 = Executors.newsinglethreadexecutor (); Create a buffer pool with a capacity of 1 executorservice Executor3 = Executors.newcachedthreadpool (); Create a buffer pool with a buffer pool capacity of integer.max_value
Here are the result outputs for the above three ways of creating:
Create a buffer pool of fixed capacity size
Executing task 0 Executing task 2 executing task 4 executing task 1 executing task 3task 4 execution completed task 5task 0 execution completed task 6task 2 execution completed task 7task 1 execution is executing Execute task 8task 3 execution completes executing task 9task 5 execution completed task 7 execution completed task 10task 6 execution completed task 11 executing task 12task 9 execution completed task 13task 8 execution completed Executing task 14task 11 completion of Task 12 completion Task 10 execution complete Task 13 execution complete Task 14 execution complete
Create a buffer pool with a capacity of 1
Executing task 0task 0 execution completes executing task 1task 1 execution completed task 2task 2 execution completed task 3task 3 execution completed task 4task 4 execution completed task 5task 5 execution is executing Execute task 6task 6 execution completes executing task 7task 7 execution completed task 8task 8 execution completed task 9task 9 execution completed task 10task 10 executing task 11task 11 execution complete Executing task 12task 12 execution finished executing task 13task 13 execution completed task 14task 14 execution completed
Create a buffer pool with a buffer pool capacity of integer.max_value
Performing task 0 executing task 2 executing task 4 executing task 6 executing task 8 performing Task 10 executing task 12 is performing a task that is executing a task that is being executed 14 is doing the tasks that are being executed. 9 Executing Task 7 executing task 11 executing task 13task 2 execution Complete task 8 execution complete Task 6 execution Complete Task 4 execution complete Task 0 Execution complete Task 10 execution completed task 14 execution complete task 1 Completion of Task 3 completion of Task 5 execution Complete Task 7 execution complete Task 9 execution complete Task 13 completion of Task 11 execution
Rejectedexecutionexception Exception:
http://blog.csdn.net/wzy_1988/article/details/38922449
Java concurrent programming--thread pool preliminary