Java multithreading-thread pool, java multithreading thread
The Thread Pool is useful for limiting the number of threads running at the same time in an application. Because each time a new thread is started, there will be corresponding performance overhead, and each thread needs to allocate some memory to the stack.
We can pass the concurrent tasks to a thread pool to start a new thread for each concurrent task. As long as there are Idle threads in the pool, the task will be allocated to a thread for execution. Within the thread pool, the task is inserted into a Blocking Queue, and the threads in the thread pool will fetch the tasks in the Queue. When a new task is inserted into the queue, a idle thread will successfully retrieve the task from the queue and execute it.
Thread pools are often used on multi-threaded servers. Each connection to the server over the network is encapsulated into a task and passed to the thread pool. Threads in the thread pool process connection requests concurrently. I will go into more details about how to implement a multi-threaded server in Java later.
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 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(); } }}
The implementation of the thread pool consists of two parts. The ThreadPool class is a public interface of the thread pool, and the PoolThread class is used to implement Sub-threads that execute tasks.
To execute a task, ThreadPool.exe cute (Runnable r) uses the Runnable implementation as the call parameter. Internally, the Runnable object is put into the Blocking Queue, waiting for the quilt thread to fetch the Queue.
An idle PoolThread will extract and execute the Runnable object from the queue. You can see the code in the PoolThread. run () method. After the execution is complete, PoolThread enters the loop and tries to pull another task from the queue until the thread ends.
You can call ThreadPool. stop () to stop ThreadPool. Internally, calling stop will first mark the isStopped member variable (true ). Then, every sub-thread in the thread pool calls the PoolThread. stop () method to stop running. Note: If the thread pool's execute () is called after stop (), the execute () method throws an IllegalStateException.
The sub-thread stops after the current task is completed. Note that this. interrupt () is called in the PoolThread. stop () method (). It ensures blocking in taskQueue. the wait () call thread in dequeue () can jump out of the wait () call (proofread Note: It can interrupt this call because interrupt is interrupted ), and an InterruptedException exception is thrown to exit the dequeue () method. This exception is intercepted and reported in the PoolThread. run () method, and then the isStopped variable is checked. Because the isStopped value is true, the PoolThread. run () method exits and the subthread ends.