Search code examples
javamultithreadingjvmthreadpool

Is there a maximum size parameter in Java FixedThreadPool?


I've been running tests were the size of a Java FixedThreadPool is modified as a variable. From researching the FixedThreadPool it seems you should be able to have as many threads in the pool as your memory will allow.

My i7 4-core machine easily handles over 4000 "normal" threads, but changing the threadpool size above 1024 deadlocks my tests. The same 1024 threadpool deadlock happens for an M1 macbook and 2-core macbook air. Is there a set max for a the ThreadPool? For sizes until then everything works fine for all machines.


Solution

  • The limit does not resides in jvm memory but in OS per user thread (process) limit. In linux or Mac OS you can check by ulimit -u. here you can find more info Maximum number of threads per process in Linux?

    When you create ThreadPoolExecutor with Executors.newFixedThreadPool( nb ) no Thread is create and only a small amount of memory is require for each thread (perhaps zero if the implementation use a LinkedList or a map to keep the Worker (Thread))

    other useful info here Maximum number of threads in a JVM?