[Java] The Java Thread Pool

A thread pool helps to constrain the number of threads that may be running in an application at any one time. As threads require system resources, having too many threads executing simultaneously will negatively impact on a system’s performance.

Threads can be represented as objects. To define what our threads will be doing, we create a class that implements the Callable interface. In this example, the thread will computer an integer. The interface, itself, provides only one method – call. This is the method that is actually executed when the thread is running. Since we have assigned the integer as the generic type in the class definition, the method must also return an integer (see the generics guide for information on generics).

 1    import java.util.concurrent.Callable;
 3    public class ExecAdd implements Callable<Integer>
 4    {
 5      private int id = 0;
 7      public ExecAdd (int threadId) 
 8      {
 9          id = threadId;
10      }
12      /**
13       * This is the method that will be executed
14       * when the thread is running.
15       */
16      @Override
17      public Integer call() throws Exception
18      {
19          int result = 1;
20          System.out.println("Thread " + id +
21                  " calculating …");
23          // Compute the result.
24          for (int value = 1; value <= id; value++)
25          {
26              result = result + value;
27          }
29          // Delay for a visible amount of time.
30          // This is just for show.
31          try {
32              Thread.sleep(2000);
33          } catch (InterruptedException e) {
34              System.out.println("Interrupted");
35          }
37          return result;
38      }
39    }

Note that the call to sleep at line 32 is only intended to make the thread last a bit longer. It will, later, cause the thread pool to fill up and force other threads to wait before executing. The creation of the thread pool in which to execute these threads and the extraction of the return values from them is done in the main method of another class.

 2    import java.util.ArrayList;
 3    import java.util.List;
 4    import java.util.concurrent.ExecutionException;
 5    import java.util.concurrent.ExecutorService;
 6    import java.util.concurrent.Executors;
 7    import java.util.concurrent.Future;
 9    public class ThreadCreator {
10      public static void main(String[] args) {

The thread pool Executors class provides several methods for creating different types of thread pools. For this example, the fixed thread pool is used. A fixed thread pool will allow up to a certain number of threads to run at any one time. To show this behaviour, the example uses a thread pool size of ten but will later give thirty threads to the pool to execute.

11          // Use a thread pool executor to generate
12          // the lists.
13          ExecutorService calcExecutor =
14              Executors.newFixedThreadPool(10);
16          // Maintains a reference to our adders.
17          ArrayList<ExecAdd> adders = new 
18              ArrayList<ExecAdd>();

The use of a list to maintain references to the threads is so that the threads will be started simultaneously (the execution of the threads comes later). When the thread pool finishes executing a thread, the return value from the thread execution is obtained through the Future object.

20          // Reference to the results.
21          List<Future<Integer>> allResults = null;

Next, we create the instances of the thirty threads that will be executed in the thread pool.

23          // Create an instance of the calculators
24          // to be executed.
25          for (int index = 0; index < 30; index++) 
26          {
27              adders.add(new ExecAdd(index));
28          }

To execute the threads, the list of the threads are given to the thread pool.

30          try {
31              // Perform the calculations.
32              allResults = calcExecutor.
33                  invokeAll(adders);
35              // Output the results.
36              for (Future<Integer> calcResult:
37                  allResults) 
38              {
39                  System.out.println(
40                          calcResult.get());
41              }

When the get() method is called, the system will wait for the thread to finish execution if it has not finished already and then return the result. All that is left is the handling of the exceptions that can possibly be thrown.

43          } catch (InterruptedException e) {
44              System.out.println("Interrupted");
45          } catch (ExecutionException e) {
46              System.out.println(
47                      "Execution exception");
48          }
50      }
51    }

When you execute the code, you should notice a slight pause in between a batch of threads. This is because we have a thread pool of ten that executes at any one time, but we gave it thirty threads!!


4 Responses to [Java] The Java Thread Pool

  1. Forrest says:

    Just wanted to say thanks for posting this code example. I found it very helpful…

  2. ricardo says:

    Thanks for the post, it help me a lot when I had to create a simple thread pool.

  3. nkswamy511 says:

    Thanks for the post, it help me in understanding thread pool

  4. Carloslib says:

    From this attention grabbing opener, you would desire to move to the next part within the introduction, in which you offer some relevant background relating to the targeted purpose belonging to the essay. This section helps the reader see why you will be focusing on this topic and makes the transition to the main point of your paper. For this reason, this is often called the “transitional” part within the introduction.
    During the example previously mentioned, the anecdote about Michelle will probably seize the reader’s attention, but the essay is simply not really about Michelle. The attention grabber would most likely get the reader thinking about how drunk driving can destroy people’s lives, however it doesn’t introduce the topic of your need to get for stricter drunk driving penalties (or whatever the real focus belonging to the paper might possibly be).
    Therefore, you will need to bridge the gap somewhere between your attention-grabber and your thesis with some transitional discussion. Within this part of your introduction, you narrow your focus on the topic and explain why the attention-grabber is relevant to the distinct area you will be discussing. You should introduce your exact topic and present any necessary background content that the reader would require in order to understand the problem you are presenting within the paper. One can also define any key terms the reader could not know.
    Continuing with the example previously mentioned, we might just move from the narrative about Michelle to some short discussion belonging to the scope in the problem of drunk drivers. We would possibly say, for example: “Michelle’s story is absolutely not isolated. Just about every calendar year XX (selection) of lives are lost due to drunk-driving accidents.” You could follow this using a short discussion of how serious the problem is and why the reader should care about this problem. This effectively moves the reader from the story about Michelle to your real topic, which might possibly be the need to get for stricter penalties for drinking and driving.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: