Concept of default Thread Pools in Java — Do they even exist ?
To know the basics of thread pools, and how can we create them, refer to my previous article.
I am going to keep this very simple — Hope this helps!
The number of threads while working in any Java application could be anything, if we are explicitly not creating thread pools and defining the number of threads in every thread pool.
There’s no defined number for a default thread pool for JVM. It’s actually the task of the OS to manage all processes and threads. A universal fact while working with threads which one might be aware of is that threads use stack as storage for it’s efficient working. Thread memory contains stack frames, local variables, method parameters. If we speak technically the number of threads depends on the stack size you have set.
A very simple explanation of the above :
Assume the default size of Ram to be 512mb. Now, if you create threads, it will occupy a memory space from that 512 mb of RAM. Assume that each thread takes 1mb of space and you create 512 threads meaning 512mb of space is consumed. Any more creation of thread will result in an out of memory exception — unable to create thread. So the number of threads for any Java app depends on your stack size.
To increase the thread size :-Xss set java thread stack size
My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.
Any machine with a modern CPU and with 1–2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.
So under 32-bit Windows, for example, where RAM occupies a space of 2GB, giving each thread a 128K stack size, you’d expect an absolute maximum of 16384 threads (=2*1024*1024 / 128).
Does Java create a stack for each thread when it is created?
Yes
Where exactly is the stack in the memory?
Not on Heap. In JVM allocated memory.
Does JVM know how many threads will be created ?
No it doesn’t.
Is multiple thread pool a solution ?
There are often times we create different Thread Pools for an application. There could be instances in an application where heavy computation is done for a business logic which takes a good amount of time . In this case, all the other requests will just wait in the queue to be picked up, because our default thread pool is working on heavy computation. Hence, in such cases, we create separate thread pools for different business logic, so that different tasks can be performed in different Thread pools instead of waiting for a single thread pool to complete it’s heavy computation.
The purpose of having separate dedicated thread pools is so that an activity doesn’t get starved for threads because other activities took all the threads. If some service has its own thread pool then it is assured of having a certain number of threads at its disposal and it’s not as sensitive to demands made by other services.
We can have different thread pools for accepting connections, running batch jobs, talking to remote api’s databases. It does reduce efficiency to some extent but makes our system robust and fault tolerant.