Thread pool with many blocked tasks

I am using a thread pool that has to do hundreds of concurrent tasks. However, tasks usually do very little computation and spend most of their time waiting for the server to respond. Therefore, if the size of the thread pool contains hundreds of threads, some of them will be active, while most of them will be waiting.

I know this is not a good practice for using thread pools, but the current design does not allow my tasks to be asynchronous so that they can return the control without waiting for the server to respond. So given this limitation, I guess my biggest problem is the increased memory consumption for thread stack space.

So, is there a way to use some kind of lightweight threads that don't consume a lot of memory?

I now have the JVM option -Xss to manage stack memory, but there seems to be no way to control this for a thread pool or thread just as opposed to changing it for all threads inside a VM, right?

Also do you have any suggestions for the best solution to my problem?

+3


source to share


2 answers


I know this is not a good practice for using thread pools

I disagree. I think this is ideal practice. Do you see problems with this approach, because otherwise the transition from standard streams is a premature optimization for me?

So, is there a way to use some kind of lightweight threads that don't consume a lot of memory?



I think you are already there. Threads are relatively lightweight and I see no reason to worry about hundreds of them unless you are running in a very limited JVM.

Also do you have any suggestions for the best solution to my problem?

Any solution I see would be much more complicated and again would be a definition of premature optimization. For example, you can use NIO and run your own stream schedule when the server response was available, but that's what you get for free with streams.

+2


source


So, is there a way to use some kind of lightweight threads that don't consume a lot of memory?

Using simple threads in a thread pool will probably be easy enough.

I now have the JVM option -Xss to manage stack memory, but there seems to be no way to control this for a thread pool or thread just as opposed to changing it for all threads inside a VM, right?

This is the maximum size for a stream. Its the size at which you want to get a StackOverFlowError and not keep running. IMHO, little use in setting this up per thread.



The thread stack uses main memory for the part actually used and virtual memory for the rest. Virtual memory is cheap if you have a 64-bit JVM. If that's a concern, I would switch to 64-bit.

Also do you have any suggestions for the best solution to my problem?

, IO. , . , , 10 000 CPU ( ). 100 1% . , .

+1









All Articles