The thread pool is useful for limiting the number of threads running at the same time in the 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.
Public classThreadPool {PrivateBlockingqueue Taskqueue =NULL; Privatelist<poolthread> threads =NewArraylist<poolthread>(); Private BooleanisStopped =false; PublicThreadPool (intNoofthreads,intmaxnooftasks) {Taskqueue=NewBlockingqueue (maxnooftasks); for(inti=0; i<noofthreads; i++) {Threads.add (NewPoolthread (taskqueue)); } for(Poolthread thread:threads) {Thread.Start (); } } Public void synchronizedExecute (Runnable Task) {if( This. isStopped)Throw NewIllegalStateException ("ThreadPool is stopped"); This. Taskqueue.enqueue (Task); } Public synchronized BooleanStop () { This. isStopped =true; for(Poolthread thread:threads) {thread.stop (); } }}
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 Multithreading-thread pool