Java threading introduction  Thread-safety  Thread methods  Interruption  Thread scheduling  Context switching  Thread priorities  sleep()  yield()  Deadlock  Threading with Swing  invokeLater()  Thread pools  CoundDownLatch  ThreadPoolExecutor  CyclicBarrier

Figure 1: Typical thread pool architecture.

Thread pools in Java

In our previous discussion of how threads work, we discussed the overhead of thread creation and the fact that there was some moderate limit on the number of simultaneous threads that we could create. When we know that our application will continually receive short-term tasks to execute in parallel, a thread pool alleviates these shortcomings. The basic idea is as follows:

  • whenever we want a job performing, instead of creating a thread for it directly, we put it on to a queue;
  • a (fairly) fixed set of long-running threads each sit in a loop pulling jobs off the queue and running them.

The overall scheme is shown in Figure 1. It's arguable whether we see the job queue is an internal part of the thread pool or as a separate unit as illustrated. We show it separately in Figure 1 simply because it's something that we can configure (e.g. to proioritise jobs). But in reality the pool and queue can be quite tightly coupled: for example, in Sun's implementation of ThreadPoolExecutor, the queue is completely bypassed when there are spare threads waiting for a job.

Aside from that argument, the diagram shows a classic scenario where the "jobs" are incoming connections to a server that need servicing (in this case, the threads are sometimes called "worker threads"). Indeed, if you're using a bog-standard application server or a Servlet runner (such as Tomcat), your application is very probably running in a thread pool architecture without you necessarily realising it.

In Java:

  • a thread pool implementation is provided in the form of the ThreadPoolExecutor class, part of the Java concurrency framework introduced in Java 5;
  • you can plug in different implementations of BlockingQueue in order to specifiy different queue behaviour such as queue bounds or priority ordering: see thread pool queues.

 Java threading articles  Java threading and concurrency  Java profiling  Java performance graph index

Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey. Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2016. All rights reserved.