Text address:Jenkov Jakob Jenkov Translator: Long source proofreading: Fang Fei
The Java thread pool is useful for limiting the number of threads running at the same time in an application. Because each new thread starts with a corresponding performance cost, each thread needs to allocate some memory to the stack, and so on.
We can pass the concurrently executing task to a thread pool, instead of starting a new thread for each concurrent execution of the task. As long as there are idle threads in the pool, the task is assigned to a thread to execute. Inside the online pool, the task is inserted into a blocking queue (Blocking queues), and thread constructor threads will fetch the tasks in the queue. When a new task is inserted into the queue, an idle thread will successfully pull the task out of the queue and execute it.
Thread pools are often applied on multi-threaded servers. Each connection that arrives at the server through the network is packaged as a task and passed to the thread pool. Threads in the thread pool handle requests on the connection concurrently. In the future, we will delve into the details of implementing multithreaded servers in Java.
Java 5 comes with a built-in thread pool in the Java.util.concurrent package, so you don't have to implement your own thread pool. You can read the java.util.concurrent.ExecutorService article I wrote to learn more about the built-in thread pool. However, it is always useful to know a little about thread pool implementations.
Here's a simple thread pool implementation:
- public class ThreadPool {
- Private Blockingqueue taskqueue = null;
- Private list<poolthread> threads = new arraylist<poolthread> ();
- Private Boolean isStopped = false;
- Public ThreadPool (int noofthreads, int maxnooftasks) {
- Taskqueue = new Blockingqueue (maxnooftasks);
- for (int i=0; i<noofthreads; i++) {
- Threads.add (New Poolthread (Taskqueue));
- }
- for (Poolthread thread:threads) {
- Thread.Start ();
- }
- }
- public void synchronized Execute (Runnable task) {
- if (this.isstopped) throw
- New IllegalStateException ("ThreadPool is stopped");
- This.taskQueue.enqueue (Task);
- }
- Public synchronized Boolean stop () {
- This.isstopped = true;
- for (Poolthread thread:threads) {
- Thread.stop ();
- }
- }
- }
Copy Code
- <span style= "Color:rgb (102, 102, 102); Font-family:arial, Helvetica, Sans-serif; line-height:35px; Widows:auto; "> (glossing: The original is compiled error, I modified the next) </span>
Copy Code
The implementation of the thread pool is made up of two parts. The class ThreadPool is the exposed interface of the thread pool, and the class Poolthread is used to implement the child threads that perform the task.
To perform a task, the method Threadpool.execute (Runnable R) uses the Runnable implementation as the invocation parameter. Internally, the Runnable object is placed in a blocking queue (Blocking), waiting for the quilt thread to take out the queue.
An idle Poolthread thread pulls the Runnable object out of the queue and executes it. You can see the code in the Poolthread.run () method. After execution, the Poolthread enters the loop and attempts to pull a task out of the queue until the thread terminates.
Call the Threadpool.stop () method to stop ThreadPool. Internally, the call to stop first marks the isStopped member variable (TRUE). Then, each child thread of the thread pool calls the Poolthread.stop () method to stop running. Note that if the thread pool's execute () is called after Stop (), the Execute () method throws an IllegalStateException exception.
The child thread stops after completing the currently executing task. Note This.interrupt () is called in the Poolthread.stop () method. It ensures that the thread that is blocking the wait () call in Taskqueue.dequeue () is able to jump out of the Wait () call (Proofing Note: Because the interrupt interrupt is executed, it can break the call) and throws a interruptedexception Exceptions leave the Dequeue () method. This exception is intercepted, reported in the Poolthread.run () method, and then checked for isStopped variables. Because the value of isStopped is true, the Poolthread.run () method exits and the child thread terminates.
"Java Thread pool"