Recently I face a interview where interviewer asked me about what will be maximum thread you can assign to thread pool. I answered him it will be depend upon hardware combination. Also I can manually test execution by increasing threads in thread-pool.
He seem to be not happy with that.
can anyone tell how can we decide what will be the maximum threads we should use for better performance. any guide link will be appreciated(In core java application)
ThreadPool can automatically increase or reduce the number of active threads to maximize task execution efficiency. The maximum allowed number of processing threads in a pool is 1023. The pool allocates a maximum of 1000 threads in an I/O operation.
How to Retrieve Maximum Thread Count. The kernel parameter threads-max controls the maximum number of threads. This parameter is defined in the file /proc/sys/kernel/threads-max. Here, the output 63704 indicates that the kernel can execute a maximum of 63,704 threads.
The number of running threads per application instance is limited to 10 420. Reaching this limit can cause performance issues.
Can anyone tell how can we decide what will be the maximum threads we should use for better performance - it's definitely not maximum amount of threads.
For the best performance, amount of threads should be equal to number of processor cores (don't forget to use -XmsYYYYM and -XmxYYYYM, without them you can face situation when your processor not assigning threads to the cores).
About maximum threads your answer was correct, it depends on hardware and OS. On linux it can be checked:
cat /proc/sys/kernel/threads-max
edited.
You can create thread pool with Integer.MAX_VALUE
But you limited with amount of maximum thread usage. On my laptop. Command "cat /proc/sys/kernel/threads-max" shows me 126987.
Code that 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: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:714) at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:949) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1360) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110) at com.stackoverflow.test.TestMaxAmountOfThreads.main(TestMaxAmountOfThreads.java:10)
So I can use only 31850 threads, without jvm tuning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With