Warm tip: This article is reproduced from stackoverflow.com, please click
java multithreading

How to avoid bottle necks when working with many threads?

发布于 2020-04-08 09:23:42

I have the following loop:

// myList is an ArrayList (10000 elements)
for(MyObject test : myList) {
    test.calculate();
}

And I thought that it is a good candidate to parallelization since every calculate() operation does not depend on anything else, it only makes some math operations using some variables within the same object.

I know doing the following will have a negative impact on performance since I will be creating 10000 threads which will create a huge queue on my 4 cores processor:

// myList is an ArrayList (10000 elements)
for(MyObject test : myList) {
    Thread thread = new Thread() {
        public void run() {
            test.calculate();
        }  
    };
    thread.start();
}

The question is, what's the recomended way to work with multiple threads on this type of scenarios to avoid queuing?

Questioner
CIOC
Viewed
64
David Siegal 2020-02-01 06:25

A simpler approach is to take advantage of Java 8 parallel streams.

myList.parallelStream().forEach( MyObject::calculate);

The JVM maintains a pool of threads for such tasks, with as many threads as there are cores (or hyperthreads) on the computer. It does not spin up a new pool every time it encounters parallel code; nor does it spin up a new thread for every iteration.

Of course, with any performance issue, do benchmarking. There is some overhead to forking work among multiple threads and then joining the results (presumably you want to capture the results). So make sure the savings outweigh the overhead.