I'm having a game server based on Java. 1 user need to use 2 threads to send and receive data. But whenever the thread come to 200-300 threads, the function where execute data doesnt work anymore. CPU, RAM of the server is not full, just around 15-20%. I tried to use "garbage collector" when user disconnect, but this still happen. Thanks for helping. Sorry with my bad English.
3 Answers
Your service, should ideally never be creating "too many threads". Opt for a thread-pool using ExecutorService.
Number of threads you want to create a pool with, depends upon the kind of underlying task you have.
From a general practice:
1: For a CPU Intensive task your number of threads should be equal to
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
2: For an IO Intensive task you can create more number of threads than the number of available processors as most of your threads will be waiting if the IO task is taking long.

- 1,626
- 1
- 14
- 21
300 Java Threads does not sound like too much (compare with default settings for application servers like Wildfly). If your application is getting stuck but neither CPU nor RAM are the bottleneck, maybe try to figure out what is happening. You may be facing threads waiting for each other to finish as well.
Thus I recommend to look at the thread dump to see where the threads might be stuck. Check out Generate a Java thread dump without restarting.

- 7,748
- 1
- 16
- 42
Try checking out java ExecutorService to create a thread pool with a fixed number of threads.

- 39
- 5