How to handle thousands of threads in Java without using the new java.util.concurrent package

java

Solution

I concur with Ryan's Answer. But the problem is worse than his analysis suggests.

Hotspot JVMs have a hard-wired minimum stack size - 128k for Java 6 and 160k for Java 7.

That means that even if you set the stack size to the smallest possible value, you'd need to use roughly twice your allocated space ... just for thread stacks.

In addition, having 30k native threads is liable to cause problems on some operating systems.

I put it to you that your task is impossible. You need to find an alternative design that does not require you to have 30k threads simultaneously. Alternatively, you need a much larger machine to run the application.

Reference: http://mail.openjdk.java.net/pipermail/hotspot-runtime-dev/2012-June/003867.html

Problem

I have a situation in which I need to create thousands of instances of a class from third party API. Each new instance creates a new thread. I start getting OutOfMemoryError once threads are more than 1000. But my application requires creating 30,000 instances. Each instance is active all the time. The application is deployed on a 64 bit linux box with 8gb RAM and only 2 gb available to my application. The way the third party library works, I cannot use the new Executor framework or thread pooling. So how can I solve this problem? Note that using thread pool is not an option. All threads are running all the time to capture events. Sine memory size on the linux box is not in my control but if I had the choice to have 25GB available to my application in a 32GB system, would that solve my problem or JVM would still choke up? Are there some optimal Java settings for the above scenario ? The system uses Oracle Java 1.6 64 bit.

Original source