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> cllbls = new ArrayList<>(); for (int i = 0; i < numberOfThreads; i++) { Callable r = new IncrementSharedLong(lv, 1, numberOfIncrements); cllbls.add(r); } // get the start time long time0 = System.nanoTime(); // give the entire collection of Callables to the ExecutorService and // it will take care of running these in threads. es.invokeAll(cllbls); // tell the ExecutorService to shut down once all of the Callables // are done es.shutdown(); // Wait for the ExecutorService to finish. // If the ES has not completed within one day of waiting, throw // an exception. (effectively, wait for ever!) es.awaitTermination(1, TimeUnit.DAYS); // all threads are now done // get the end time long time1 = System.nanoTime(); // return the elapsed time for running the threads return time1-time0; }

};

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!