Calculate the maximum number of threads that can be used to improve performance in Threadpool

I recently came across an interview in which the interviewer asked me what would be the maximum thread that you can assign to a thread pool. I told him that it would depend on the hardware combination. I can also manually test the execution by increasing the threads in the thread pool.

He seems not happy with this.

can someone tell how we can decide what the maximum flows will be, which we should use to increase productivity. any reference link would be appreciated (in the main Java application)

+7
java multithreading
source share
3 answers

Can anyone tell how we can decide what the maximum threads will be, which we should use to improve performance - this is definitely not the maximum number of threads.

For best performance, the number of threads should be equal to the number of processor cores (do not forget to use -XmsYYYYM and -XmxYYYYM , without them you may encounter a situation where your processor does not assign threads to the cores.)

About maximum flows, your answer was correct, it depends on the hardware and OS. In linux, you can check:

cat /proc/sys/kernel/threads-max 

edited.

You can create a thread pool with Integer.MAX_VALUE

But you have limited the amount of maximum use of threads. On my laptop. The command "cat / proc / sys / kernel / threads-max" shows me 126987 .

The code I ran:

 package com.stackoverflow.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestMaxAmountOfThreads { public static void main(String[] args) { ExecutorService serivce = Executors.newFixedThreadPool(Integer.MAX_VALUE); for (int i = 0; i < Integer.MAX_VALUE; i++) { serivce.submit(new Runnable() { public void run() { try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { } } }); System.out.println(i); } } } 

Output:

31850

Exception in thread "main" java.lang.OutOfMemoryError: cannot create a new native thread in java.lang.Thread.start0 (Native Method) in java.lang.Thread.start (Thread.java:714) in java.util.concurrent .ThreadPoolExecutor.addWorker (ThreadPoolExecutor.java:949) in java.util.concurrent.ThreadPoolExecutor.execute (ThreadPoolExecutor.java:1360) in java.util.concurrent.AbstractExecutorService.submit (AbstractExecutorService.java test.TestMaxAmountOfThreads.main (TestMaxAmountOfThreads.java:10)

Therefore, I can only use 31850 tags, without configuring jvm.

+5
source share

ThreadPoolExecutor class constructor definition as shown below -:

 public ThreadPoolExecutor(int corePoolSize,**int maximumPoolSize**,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) 

The maximum value of integer ( Integer.MAX_VALUE ) can be assigned to maximumPoolSize. Setting the thread pool too large can lead to performance problems. If too many threads start at the same time, the priority of task switching becomes a significant factor.

0
source share

Calculate the Maximum Number of Threads You Can Use to Improve Performance in Threadpool

The answer to this question depends entirely on what task each thread will perform.

On the one hand, if your program is tied to a CPU , then the number of running threads should not be higher than the number of processor cores. Some Intel processors have a "Hyper Threading" feature, which is known to affect CPU-bound tasks , so you should consider disabling Hyper Threading altogether.

On the other hand, if your program is not connected to the CPU, but IO binding is the whole, then the number of threads should not exceed 2 or 3 times more available cores. If you have 8 cores, you should not go above 24 threads for an IO-related program.

In the middle, if your thread pool needs to perform mixed operations with CPU and IO bindings, you should test and measure performance with a different output and decide a reasonable number for your specific case. There is no theoretical way to find a specific number, you should run some specific tests in your specific application.

This, of course, is entirely up to your program, and if you have a program that is completely IO bound, then I would say that you stick to just one thread and use the non-blocking asynchronous IO API available on major platforms. In Java, we have Non-Blocking IO (NIO) .

0
source share

All Articles