Using thread pools for asynchronous processing

Thread pool is a core concept in multithreaded programming that serves a collection of idle threads that can be used to execute a task. A thread pool can reuse previously created threads to execute the current task so that the thread is already available when the request arrives, which can reduce the time of thread creation and improve the performance of the application. Normally, thread pool can be used in a web server to handle client requests and also to maintain open connections to the database.

We can configure the maximum number of concurrent threads in the pool, which is useful for preventing overload. If all threads are busy executing a task, then a new task is placed in a queue and waits for a thread to become available.

The Java concurrency API supports the following types of thread pools:

  • Fixed-thread pool: A thread pool with a fixed number of threads. A task will only execute if a thread is available, otherwise, it is waiting in a queue. The Executors.newFixedThreadPool() method is used to create a fixed-thread pool.
  • Cached-thread pool: A thread pool where we can create new threads as required, but also reuse previously created threads. A thread will be terminated and removed from the pool if it is ideal for 60 seconds. The Executors.newCachedThreadPool() method is used to create a cached-thread pool.
  • Single-thread pool: A thread pool with one thread. It executes tasks one by one. The Executors.newSingleThreadExecutor() method is used to create a single-thread pool.
  • Fork/join pool: A thread pool that is used to perform heavy tasks faster, by splitting the task into smaller pieces recursively. To create a fork/join pool, we need to create an instance of the ForkJoinPool class.

The following is a simple example of the fixed-thread pool:

public class ThreadPoolExample {
private static final Logger LOGGER =
Logger.getLogger(ThreadPoolExample.class);
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);

for (int i = 1; i <= 6; i++) {
Runnable task = new Task(" " + i);
executor.execute(task);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
LOGGER.info("All threads finished");
}
}

The following demonstrates how the task is implemented:

public class Task implements Runnable {
private static final Logger LOGGER = Logger.getLogger(Task.class);
private String taskNumber;

public Task(String taskNumber) {
this.taskNumber = taskNumber;
}

@Override
public void run() {
LOGGER.info(Thread.currentThread().getName() + ", Execute Task = "
+ taskNumber);
taskProcess();
LOGGER.info(Thread.currentThread().getName() + ", End");
}

private void taskProcess() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

In the previous example, we created a pool with a maximum of three concurrent threads and submitted 6 tasks to the executor object. When we compile and run the preceding class, we know that only three threads execute the tasks.

Here is the output:

pool-1-thread-1, Execute Task = 1
pool-1-thread-2, Execute Task = 2
pool-1-thread-3, Execute Task = 3
pool-1-thread-1, End
pool-1-thread-1, Execute Task = 4
pool-1-thread-3, End
pool-1-thread-2, End
pool-1-thread-2, Execute Task = 5
pool-1-thread-3, Execute Task = 6
pool-1-thread-1, End
pool-1-thread-2, End
pool-1-thread-3, End
All threads finished
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset