Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Pool performance

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);
        }

    }

}

}
like image 667
Peter Avatar asked Feb 11 '23 22:02

Peter


2 Answers

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?

  1. use the same one person you used above
  2. ask that the person to bring a friend to write the "two"s. Except that the rules are that they start at one end of a hall, and both run to the paper. The one who gets there first, writes their word. Now they run back to the other end of the hall and repeat the process until all the words are written.

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!

like image 141
Boris the Spider Avatar answered Feb 15 '23 11:02

Boris the Spider


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.

like image 45
Peter Lawrey Avatar answered Feb 15 '23 10:02

Peter Lawrey