Java thread pool

Source: Internet
Author: User

Text address:Jenkov Jakob Jenkov Translator: Long source proofreading: Fang Fei

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 a concurrently executed 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 Queue  ), Thread constructor threads are going to fetch the tasks in this 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 pools. You can read my written  java.util.concurrent.executorservice   's article 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:

  1. public class ThreadPool {


  2. Private Blockingqueue taskqueue = null;

  3. Private list<poolthread> threads = new arraylist<poolthread> ();

  4. Private Boolean isStopped = false;


  5. Public ThreadPool (int noofthreads, int maxnooftasks) {

  6. Taskqueue = new Blockingqueue (maxnooftasks);


  7. for (int i=0; i<noofthreads; i++) {

  8. Threads.add (New Poolthread (Taskqueue));

  9. }

  10. for (Poolthread thread:threads) {

  11. Thread.Start ();

  12. }

  13. }


  14. public void synchronized Execute (Runnable task) {

  15. if (this.isstopped) throw

  16. New IllegalStateException ("ThreadPool is stopped");


  17. This.taskQueue.enqueue (Task);

  18. }


  19. Public synchronized Boolean stop () {

  20. This.isstopped = true;

  21. for (Poolthread thread:threads) {

  22. Thread.stop ();

  23. }

  24. }


  25. }

Copy Code

    1. <span style= "Color:rgb (102, 102, 102); Font-family:arial, Helvetica, Sans-serif; line-height:35px; Widows:auto; Background-color:rgb (255, 255, 255); " > (glossing: Original compilation error, I modified the next) </span>

Copy Code

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.

In order to perform a task, the method Threadpool.execute (Runnable R) uses the Runnable implementation as the invocation parameter. Internally, the Runnable object is put into   blocking queue (Blocking queue) , Waiting for the quilt thread to take out the queue.

An idle Poolthread thread takes 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.