Objective
In Java concurrent programming, the thread pool is a very common technology to maintain thread closure, of course, in our web development, the most familiar is the JDBC connection pool, then what is the relationship between these two pools?
The answer is obviously no, the predecessor is to implement the thread security needs, the latter is to reuse the system resource needs. Let's talk about thread pooling first.
Thread pool
Literally, a pool is a resource pool that manages a small group of homogeneous work. It is closely related to the work queue, which is the queue that holds all the waiting tasks to be executed. The thread that executes each task is called the worker thread, and its task is to pick up a task in the queue and then go back to the thread pool and wait for the next task.
That's the problem: Why not open a thread for each task, and that's pretty much the point, the key to this problem is not realizing that it takes a lot of overhead to create threads and destroy threads, and that it takes time for a task to come up and create a thread, which causes a delay in the execution of the task. And these two problems are solved by using the thread pool altogether.
How to create a thread pool:
In Java's concurrent package, executors provides a convenient configuration of thread pooling. As for some of the methods provided by this object, you can find them yourself.
The executor framework uses runnable as a basic task representation, which is a very limited abstraction that cannot return a value or fail to throw a check exception. For a Task object, there are generally computational costs or I/o costs, and for these tasks, callable is a better package than runnable. All of Executor's submit methods return a future to describe the task.
The future represents the life cycle of a task, providing a way to determine whether the task has been completed, whether the task has been canceled, the result of the task, the task being canceled, and so on.
Mina thread pool
Let's discuss the thread pool of the Mina framework below:
Mina uses a thread pool mainly in four places:
1.ioacceptor/ioconnector thread Pool
These two are inherited from the Ioservice interface of the abstract implementation class Abstractioservice, in Abstractioservice, there are:
You can see that it uses the Newcachedthreadpool (), which provides a variable-sized thread pool. Ioacceptor opens a thread for each listening port, and Ioconnector opens a thread for each connection that is established. Their threads are all taken from the thread pool.
2.IoProcess thread Pool
After each good one connection, a iosession is generated so that iosession can be considered a task list, waiting for execution (read or write), and ioprocess acting as the performer's role. We can specify the size of the ioprocess thread pool when Ioservice is created, as follows:
If we do not specify, then take the default value, as follows:
Ioprocess actually implements the thread pool by maintaining an array, and the object in the array is actually taken out of the newcachedthreadpool, as for why it is not newfixedthreadpool.
Ioprocess the lifetime of a thread depends on the task it performs, and the task he performs is to complete a socket conversation, which is the life cycle of the iosession.
3.IoHandler thread Pool
After the data has been codec, it is sent to handler to perform business logic operations, which are handled by the ioprocess thread, if there are very time-consuming operations in handler, such as disk I/O, complex computations, etc. This causes the ioprocess thread pool to be unable to reclaim these threads, and if there is a large number of socket connections, it will be waiting.
In fact, we can add a threadpool to the filter chain so that the processing in handler is given to another thread pool, and ioprocess can be returned to the original pool.
<Java> Mina thread pool