Thread pool performance

I am trying to understand the advantage of using thread pools, I wrote this code to see the increase in fixed thread pool time.

First I set the number of threads in the pool to 1 and took about 920ms, then I changed the number of threads in the pool to 2 (and 3,4,5,6,7 ...) and it took 1200ms, shouldn't be faster when Are the threads running concurrently?

When I changed it to a cached thread pool, it also took 1200ms

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

    }

}

}

      

+3


source to share


2 answers


You are asking many threads for all to perform one action, which is most likely (although not guaranteed) synchronized

:

System.out.println(number);

      

Suppose 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" every 10 times on a piece of paper with one word on each line; which will be faster?



  • use the same person you used above.
  • ask the person to bring a friend to write "two." Except that the rules are that they start at one end of the hall and both run to paper. The one who gets there first writes his word. They now run back to the other end of the room and repeat the process until all the words have been written down.

I would compromise that the first alternative would be faster.

Imagine the same scenario where 5 people all try to write their word 10,000 times? Chaos? Yes!

If you want to see real improvements from streaming, tasks should be completely isolated without synchronization. And especially IO!

+5


source


When you have content resources, the optimal number of threads might be 1. Writing to the console is expensive and single-threaded, so when you use multiple threads, you are adding overhead rather than speeding up your program. Threads work best when they work independently.



+1


source







All Articles