Java Reference
In-Depth Information
can be reused to execute another task. You have a number of options to decide on what kind of thread pool to use for
an executor:
You can use one of the factory methods of the
Executors class to get an executor, which
has a preconfigured thread pool and lets you reconfigure it, if you desire so. You will use
this approach to get an executor in your examples. You can also use this class to get a
preconfigured executor that cannot be reconfigured. The commonly used methods of the
Executors class to get an executor service are as follows:
newCachedThreadPool() : It returns an ExecutorService object. The thread pool reuses
the previously created threads if they are available. Otherwise, it creates a new thread to
execute a task. It destroys and removes idle threads from the pool. The thread pool has
characteristics of expanding and shrinking depending on the workload.
newFixedThreadPool(int nThreads) : It returns an ExecutorService object. The thread
pool maintains a fixed number of threads. At any time, the thread pool will have the
maximum nThread number of threads. If a task arrives in the work queue and all threads
are busy executing other tasks, the task has to wait for its execution until a thread
becomes available. If a thread is terminated because of an unexpected failure during a
task execution, it is replaced with a new thread.
newSingleThreadExecutor() : It returns an ExecutorService object. The thread pool
maintains only one thread to execute all tasks. It guarantees that only one task will be
executed at a time. If the lone thread dies unexpectedly, it is replaced with a new one.
ThreadPoolExecutor class and configure the thread pool.
You can instantiate the
You can create your own executor from scratch.
Listing 6-47 and Listing 6-48 have the complete programs for an executor. An object of the RunnableTask class
represents a task in your program. You will have a task that will sleep for some time and print a message on the
standard output. The time to sleep will be determined randomly between 1 and 10 seconds. The task will repeat
three times in its run() method. You have used an executor with its thread pool with a fixed number of threads. Your
executor will have only three threads in its thread pool to execute only three tasks at a time. When the executor is
done with one of the first three tasks, it starts the fourth one. Note the exec.shutdown() method call to shut down the
executor after submitting all tasks. The shutdownNow() method call of executor attempts to stop the executing tasks by
interrupting it and discards the pending tasks. It returns the list of all pending tasks that were discarded.
Listing 6-47. A Runnable Task
// RunnableTask.java
package com.jdojo.threads;
import java.util.Random;
public class RunnableTask implements Runnable {
private int taskId;
private int loopCounter;
private Random random = new Random();
public RunnableTask(int taskId, int loopCounter) {
this.taskId = taskId;
this.loopCounter = loopCounter;
}
 
Search WWH ::




Custom Search