Sun has made a lot of extensions to Java Thread class libraries in Java 5. Among them, the thread pool is one of the new features of Java 5. Besides the thread pool, there are also a lot of multithreading-related content, it brings great convenience to multi-threaded programming. To write efficient, stable, and reliable multi-threaded programs, it is particularly important to add content to the thread section.
All content about the new features of Java 5 threads is under java. util. Concurrent, which contains a large number of interfaces and classes. familiarity with these API features is a difficult learning process. At present, there are few materials and books related to this aspect. Books about thread are still on the knowledge level before Java 5.
Of course, the new feature does not have to do with multi-threaded programs. Before Java 5, you can write excellent multi-threaded programs. The price is different.
The basic idea of the thread pool is also the idea of an object pool, which opens up a piece of memory space and stores a large number of threads (not dead). The thread execution scheduling in the pool is handled by the pool manager. When there is a thread task, take one from the pool. After the execution is complete, the thread object is returned to the pool. This avoids the performance overhead caused by repeated creation of thread objects and saves system resources.
Before Java 5, it is quite difficult to implement a thread pool. Now Java 5 has done everything for us, and we only need to use it according to the provided API, you can enjoy the great convenience brought by the thread pool.
Java 5 thread pools are divided into various types: fixed-size thread pools, variable-size connection pools, and ,.
Before using the thread pool, you must know how to create a thread pool. in Java 5, you must understand Java. util. concurrent. executors class API. This class provides a large number of static methods for creating connection pools.
I. fixed thread pool size
Import java. util. Concurrent. executors;
Import java. util. Concurrent. executorservice;
/**
* Java thread: Thread Pool-
*
* @ Author administrator 23:30:44
*/
Public class test {
Public static void main (string [] ARGs ){
// Create a thread pool with a fixed number of reusable threads
Executorservice pool = executors. newfixedthreadpool (2 );
// Create and implement the runnable interface object, and the thread object also implements the runnable interface.
Thread T1 = new mythread ();
Thread t2 = new mythread ();
Thread T3 = new mythread ();
Thread t4 = new mythread ();
Thread T5 = new mythread ();
// Put the thread into the pool for execution
Pool.exe cute (T1 );
Pool.exe cute (T2 );
Pool.exe cute (T3 );
Pool.exe cute (T4 );
Pool.exe cute (T5 );
// Close the thread pool
Pool. Shutdown ();
}
}
Class mythread extends thread {
@ Override
Public void run (){
System. Out. println (thread. currentthread (). getname () + "is being executed... ");
}
}
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Process finished with exit code 0
Ii. single-task Thread Pool
The code for creating a pool object on the basis of the above example is as follows:
// Create an executor that uses a single worker thread to run this thread in the unbounded queue mode.
Executorservice pool = executors. newsinglethreadexecutor ();
Output result:
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Process finished with exit code 0
For the above two connection pools, the size is fixed. When the thread (or task) of the pool to be added exceeds the maximum size of the pool, the thread pool needs to wait in queue.
Once there are threads in the pool, a thread waiting in queue will be executed in the pool.
3. Variable-size Thread Pool
Similar to the above, we only changed the pool creation method:
// Create a thread pool where new threads can be created as needed, but they will be reused when previously constructed threads are available.
Executorservice pool = executors. newcachedthreadpool ();
Pool-1-thread-5 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-4 is being executed...
Pool-1-thread-3 is being executed...
Pool-1-thread-2 is being executed...
Process finished with exit code 0
Iv. Delayed connection pool
Import java. util. Concurrent. executors;
Import java. util. Concurrent. scheduledexecutorservice;
Import java. util. Concurrent. timeunit;
/**
* Java thread: Thread Pool-
*
* @ Author administrator 23:30:44
*/
Public class test {
Public static void main (string [] ARGs ){
// Create a thread pool that can be scheduled to run commands or periodically after a given delay.
Scheduledexecutorservice pool = executors. newscheduledthreadpool (2 );
// Create and implement the runnable interface object, and the thread object also implements the runnable interface.
Thread T1 = new mythread ();
Thread t2 = new mythread ();
Thread T3 = new mythread ();
Thread t4 = new mythread ();
Thread T5 = new mythread ();
// Put the thread into the pool for execution
Pool.exe cute (T1 );
Pool.exe cute (T2 );
Pool.exe cute (T3 );
// Method using the delayed execution style
Pool. Schedule (T4, 10, timeunit. milliseconds );
Pool. Schedule (T5, 10, timeunit. milliseconds );
// Close the thread pool
Pool. Shutdown ();
}
}
Class mythread extends thread {
@ Override
Public void run (){
System. Out. println (thread. currentthread (). getname () + "is being executed... ");
}
}
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Process finished with exit code 0
5. single task delay connection pool
Make changes based on the four Codes
// Create a single-threaded execution program, which can be scheduled to run commands or regularly after a given delay.
Scheduledexecutorservice pool = executors. newsinglethreadscheduledexecutor ();
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-1 is being executed...
Process finished with exit code 0
6. Custom Thread Pool
Import java. util. Concurrent. arrayblockingqueue;
Import java. util. Concurrent. blockingqueue;
Import java. util. Concurrent. threadpoolexecutor;
Import java. util. Concurrent. timeunit;
/**
* Java thread: Thread Pool-custom Thread Pool
*
* @ Author administrator 23:30:44
*/
Public class test {
Public static void main (string [] ARGs ){
// Create a waiting queue
Blockingqueue bqueue = new arrayblockingqueue (20 );
// Create a single-threaded execution program, which can be scheduled to run commands or regularly after a given delay.
Threadpoolexecutor pool = new threadpoolexecutor (2, 3, 2, timeunit. milliseconds, bqueue );
// Create and implement the runnable interface object, and the thread object also implements the runnable interface.
Thread T1 = new mythread ();
Thread t2 = new mythread ();
Thread T3 = new mythread ();
Thread t4 = new mythread ();
Thread T5 = new mythread ();
Thread T6 = new mythread ();
Thread T7 = new mythread ();
// Put the thread into the pool for execution
Pool.exe cute (T1 );
Pool.exe cute (T2 );
Pool.exe cute (T3 );
Pool.exe cute (T4 );
Pool.exe cute (T5 );
Pool.exe cute (T6 );
Pool.exe cute (T7 );
// Close the thread pool
Pool. Shutdown ();
}
}
Class mythread extends thread {
@ Override
Public void run (){
System. Out. println (thread. currentthread (). getname () + "is being executed... ");
Try {
Thread. Sleep (100l );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Pool-1-thread-2 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Pool-1-thread-1 is being executed...
Pool-1-thread-2 is being executed...
Process finished with exit code 0
There are many methods to create a custom thread pool. In this example, the parameters are described as follows:
Threadpoolexecutor
Public threadpoolexecutor (INT corepoolsize,
Int maximumpoolsize,
Long KeepAliveTime,
Timeunit unit,
Blockingqueue workqueue)
Create a new threadpoolexecutor with the given initial parameters and the default thread factory and handler. Using the executors factory method is much easier than using this General constructor method.
Parameters:
Corepoolsize-Number of threads stored in the pool, including Idle threads.
Maximumpoolsize-Maximum number of threads allowed in the pool.
KeepAliveTime-when the number of threads exceeds the core, this is the maximum time for Idle threads to wait for new tasks before termination.
Unit-KeepAliveTime parameter time unit.
Workqueue-the queue used to keep the task before execution. This queue only keeps runnable tasks submitted by the execute method.
Throw:
Illegalargumentexception-If corepoolsize or KeepAliveTime is less than zero, maximumpoolsize is less than or equal to zero, or corepoolsize is greater than maximumpoolsize.
Nullpointerexception-If workqueue is null
The custom connection pool is a little complicated, but the threadpoolexecutor thread pool object can be used to obtain the size of the current thread pool, the number of threads executing tasks, and the work queue.
The content about the Java 5 thread pool is no longer available here. More information needs to be obtained through the API.