Question: In a cached thread pool threads are created and reused. That is, if a thread finishes and there are still runnable objects waiting to run,
In a cached thread pool threads are created and reused. That is, if a thread finishes and there are still runnable objects waiting to run, the thread can be used for the runnable This avoids creating and managing new threads, which can be a signiicant saving. There is no fixed limit on the number of threads, though the implementation may limit the number based on local conditions.
In a fixed thread pool, the application sets the maximum number of threads to use. Threads are still reused as in the cached pool.
Copy CachedThreadPool and modify the copy to create a programFixedThreadPool. The modification is to use the following command when creating the ExecutorService
ExecutorService es = Executors.newFixedThreadPool(threadPoolLimit);
You will also need to define a variable threadPoolLimit and give it a value.
Run the program with different values for threadPoolLimit:
Set threadPoolLimit to the constant 1
Try other several other constants up to 64, say.
Set threadPoolLimit to the number of threads
Set threadPoolLimit to half the number of threads
Discuss how these different choices compare with each other. Also, how does a fixed thread pool compare with a serial execution.
cashed thread pool code:
package sharedVariable;
import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;
/** * Create an ExcecutorService that will manage threads for us. * We just create the Callable objects (like Runnable but the method returns a value, * which we ignore in this project) and hand them to the ExecutorService. * * The particular ExecutorService used here is a CachedThreadPool which means it * will keep thread objects once they complete and reuse them if there are any other * Callables needing threads still. */ public class CachedThreadPool {
private static IncrementRunner cached = new IncrementRunner() { /** * Runs multiple threads incrementing the given variable. * Returns time of execution. * * @param lv Variable to increment * @param numberOfIncrements Number of times each thread increments the variable * @param numberOfThreads Number of threads to use * @return Amount of time taken, in nanoseconds. * @throws InterruptedException */ @Override public long runIncrements(LongVariable lv, long numberOfIncrements, int numberOfThreads) throws InterruptedException {
// The ExecutorService is produced by a 'factory method' rather // than using 'new' directly on a class. ExecutorService es = Executors.newCachedThreadPool(); // create a collection off the Callables to be used later Collection
};
public static void main(String[] args) throws InterruptedException { int numberOfThreads = 1; long numberOfIncrements = 1024*1024*2; cached.doRun(numberOfThreads, numberOfIncrements, 4); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
