I'm trying to understand the advantage of using thread pools, i wrote this code to see the time improvement of fixed thread pool.
First, i set the number of threads in the pool to 1 and it took approximately 920 ms, then i changed the number of threads in the pool to 2(and 3,4,5,6,7...) and it took 1200 ms, shouldn't it be faster when the threads are running concurrently?
When i changed it to cached thread pool it also took 1200 ms
package threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Example3 {
public static void main(String[] args) {
new Example3();
}
public Example3() {
try {
testFixedPool(1);
//testFixedPool(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testFixedPool(int numberOfThreads) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
long start = System.currentTimeMillis();
executor.execute(new TaskPrintInteger(0, 10000));
executor.execute(new TaskPrintInteger(1, 10000));
executor.execute(new TaskPrintInteger(2, 10000));
executor.execute(new TaskPrintInteger(3, 10000));
executor.execute(new TaskPrintInteger(4, 10000));
executor.execute(new TaskPrintInteger(5, 10000));
executor.execute(new TaskPrintInteger(6, 10000));
executor.execute(new TaskPrintInteger(7, 10000));
executor.execute(new TaskPrintInteger(8, 10000));
executor.execute(new TaskPrintInteger(9, 10000));
executor.shutdown();
while(!executor.isTerminated()){
}
System.out.println();
System.out.println((System.currentTimeMillis()) - start);
}
private class TaskPrintInteger implements Runnable {
private int number, times;
public TaskPrintInteger(int number, int times) {
this.number = number;
this.times = times;
}
@Override
public void run() {
for (int i = 0; i < times; i++) {
System.out.println(number);
}
}
}
}
You are asking many threads to all carry out one activity, that is most likely (although not guaranteed to be) synchronized
:
System.out.println(number);
Lets imagine that you have one person
, and you ask her to write "one" 10 times on a piece of paper, one word on each line.
Now, you need to write "one" and "two" each 10 times on a piece of paper with one word on each line; which will be faster?
I would hazard a guess that the first alternative would be faster.
Imagine the same scenario with 5 people all trying to write their word 10,000 times? Chaos? Yes!
If you want to see real improvements from threading, tasks should be completely isolated with no synchronization. And especially no IO!
When you have a contented resources, the optimal number of threads can be 1. Writing to the console is expensive and single threaded, so when you use multiple threads you are increasing the overhead, rather than making your program faster. Threads work best when they run independently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With