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.
CodePudding user response:
Try checking out java ExecutorService to create a thread pool with a fixed number of threads.
CodePudding user response:
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.