Java Thread Optimization with 100% CPU Usage

I have an application that accepts work in a queue and then spins this work for independent threads. The number of threads is not massive, say, up to 100, but these are intensive tasks and can quickly increase processor performance up to 100%.

To get the job done as efficiently as possible: is it better for me to start more threads, when I need to do more work, and let the Java thread scheduler handle the work, or will it be smarter and manage the workload, will a processor below 100% speed me up?

The machine is dedicated to my java application.

EDIT:

Thanks for the fantastic contribution!

Tasks of varying complexity also include I / O operations, so having a pool with a small number of threads, say 4, will probably only work with a CPU of up to 20%. I have no way of knowing how many tasks the processor actually takes up to 100%.

My thought was that I should control the CPU through RMI and dynamically dial up and down, or I just don’t need to let the OS handle it.

+1
source share
5 answers

, . , N (), N . , - , . , - , , , , .. .

+12

: , , Java , CPU 100% ?

, , , , , JVM. , , , . , , . , . JVM , , (, ) GC . , , .

, IO, JVM. .

Executors.newFixedThreadPool(SOME_NUMBER); . , , , .

, , , . , -. , , , .

+6

, 100%, , . , , 100% ( 100 ), .

:

private final int NUM_THREADS = Runtime.getRuntime().availableProcessors() + 1;
private final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);

, , .

, - , , .

@MartinJames, () - , size = + 1 100 ( 5%) - (1000 10000) .

- 10 :
: 9: 238 . // (NUM_CORES + 1)
: 100: 245 .
: 1000: 319 .
: 10000: 2482 .

:

public class Test {

    private final static int NUM_CORES = Runtime.getRuntime().availableProcessors();
    private static long count;
    private static Runnable r = new Runnable() {

        @Override
        public void run() {
            int count = 0;
            for (int i = 0; i < 100_000; i++) {
                count += i;
            }
            Test.count += count;
        }
    };

    public static void main(String[] args) throws Exception {
        //warmup
        runWith(10);

        //test
        runWith(NUM_CORES + 1);
        runWith(100);
        runWith(1000);
        runWith(10000);
    }

    private static void runWith(int poolSize) throws InterruptedException {
        long average = 0;
        for (int run = 0; run < 10; run++) { //run 10 times and take the average
            Test.count = 0;
            ExecutorService executor = Executors.newFixedThreadPool(poolSize);
            long start = System.nanoTime();
            for (int i = 0; i < 50000; i++) {
                executor.submit(r);
            }
            executor.shutdown();
            executor.awaitTermination(10, TimeUnit.SECONDS);
            long end = System.nanoTime();
            average += ((end - start) / 1000000);
            System.gc();
        }
        System.out.println("Pool size: " + poolSize + ": " + average / 10 + " ms.  ");
    }
}
+5

' , 100%, ?'

, .

, 100 threadpool, . - 4 400.

100 ? 16, ?

' , , 100' - ? 16 - .

- , ?

+1

You should keep 100% usage, but with a minimum number of threads. 100 threads are too many.

0
source

All Articles