Reputation: 41
I'm using a thread pool that should be able to execute hundreds of concurrent tasks. However the tasks usually do very little computation and spend most of their time waiting on some server response. So if the thread pool size contains hundreds of threads just a few of them will be active while most of them will be waiting.
I know in general this is not a good practice for thread pools usage but the current design does not permit making my tasks asynchronous so that they can return the control without waiting for the server's response. So given this limitation I guess my biggest problem is increased memory consumption for the threads stack space.
So is there any way to use some kind of light-weight threads that does not consume much memory?
I now there's a JVM option -Xss to control the stacks memory but it's seems there's no way to control this per thread pool or thread only as opposed to changing it for all the threads inside the VM, right?
Also do you have any suggestions for a better solution to my problem?
Upvotes: 3
Views: 1768
Reputation: 116828
I know in general this is not a good practice for thread pools usage
I disagree. I think this is a perfect practice. Are you seeing problems with this approach, because otherwise, switching from standard threads, smacks of premature optimization to me.
So is there any way to use some kind of light-weight threads that does not consume much memory?
I think you are already there. Threads are relatively lightweight already and I see no reason to worry about hundreds of them unless you are working in a very constrained JVM.
Also do you have any suggestions for a better solution to my problem?
Any solution that I see would be a lot more complicated and would again be the definition of premature optimization. For example, you could use NIO and do your own scheduling of the thread when the server response was available but this is the sort of thing that you get for free with threads.
Upvotes: 4
Reputation: 533442
So is there any way to use some kind of light-weight threads that does not consume much memory?
Using plain threads in a thread pool is likely to be light weight enough.
I now there's a JVM option -Xss to control the stacks memory but it's seems there's no way to control this per thread pool or thread only as opposed to changing it for all the threads inside the VM, right?
This is the maximum size per thread. Its the size at which you want to get a StackOverFlowError rather than keep running. IMHO, There is little benefit in tuning this on a per thread basis.
The thread stack uses main memory for the portion which is actually used and virtual memory for the rest. Virtual memory is cheap if you have a 64-bit JVM. If this is a concern I would switch to 64-bit.
Also do you have any suggestions for a better solution to my problem?
If you have thousands of threads you might consider using non-blocking IO. It doesn't sound like you need to worry. In tests I have done, having 10,000 active threads consumes one CPU (if the threads are otherwise not doing anything) For every hundred threads, you could be wasting 1% of one CPU. This is unlikely to be a problem if you have spare CPU.
Upvotes: 2