Reputation:
ExecutorService threadPool = Executors.newFixedThreadPool(N);
for (Runnable task : tasks) {
threadPool.execute(task);
}
I'm a bit new to multi-threading. This is a question that I got in a recent interview. Can someone help me with the solution and a clear explanation. In the code above each task spends 25% time doing computation and 75% time doing I/O. Assuming a quad core machine (no hyper threading), what should be the size of thread pool N to achieve optimal performance without wasting threads assuming infinite I/O capacity.
Upvotes: 3
Views: 1323
Reputation: 38910
If IO is not a constraint ( due to infinite IO capacity), you can concentrate on number of cores available to you.
ExecutorService threadPool = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
The performance will be further improved if you use newWorkStealingPool in Executors (from java 8 release)
public static ExecutorService newWorkStealingPool()
Creates a work-stealing thread pool using all available processors as its target parallelism level.
Upvotes: 1
Reputation: 2758
If your machine has infinite I/O it means, that you can concentrate fully on CPUs. Each task is using one quarter of a CPU while running. That means, you can have four tasks running to saturate one CPU core and that makes N=16 on a quad core machine.
This however is a purely theoretical answer. In reality, you will find several issues why N=16 might be too large or too small. As an example, assume that all 16 tasks are identical in their scheduling (CPU vs IO) and are started at the very same moment. That would mean, that in the first CPU intense time-frame execution-speed is reduced to one fourth (16 threads are fighting for four CPUs). Also, one would assume, that some amount of CPU load is required for running the OS, the scheduler, garbage-collection and the like. This would make N=16 too large.
On the other hand, if it is no requirement for each task to run with the maximum individual speed, a larger N might give you better overall performance, for example if, at a certain time-frame more then 3/4 of the threads are doing I/O it would leave CPU resources unused at that very moment.
I assume, that this is not the point of the interview-question, yet something to be taken in consideration in the real world.
Upvotes: 1