What is the best way to limit the number of threads executing a particular method?
Situation
- I have a web application.
- I have a class that complicates the math calculation
- The equations can run from time to time depending on which query
- Sometimes, many threads run this computation at the same time.
- When too much computation started, the computer hung up (freeze completely = 99 CPU usage)
My goal
My goal is to avoid hovering / freezing.
I guess it can be done by limiting the number of concurrent computations (possibly down to NUMBER_OF_CPU_CORES - 1)
Question
What is the best way to achieve this goal?
I know there is a java.util.concurrent.Semaphore , but maybe there is a better approach?
source to share
Take a look at Java ThreadPoolExecutor This will help you with what you are trying to do.
Hope it helps ...
source to share
You should probably configure your application container with a limited number of request threads required.
Banning what Semaphore
is the perfect tool. Use the method tryAcquire()
and be sure to put the appropriate version in the block finally
, for example:
if (permits.tryAcquire(7, TimeUnit.SECONDS))
try {
/* Do your computation. */
compute();
} finally {
permits.release();
}
else
/* Respond with "Too busy; try later," message. */
source to share
Decrease the priority of threads calling your method. Unless the rest of the apps on your box are CPU intensive, it is unlikely to affect your computations, but responses to keystrokes, etc. They will still be good.
In fact, I am surprised that the box will freeze and freeze even when the processor is overloaded from several ready threads (if their priority is not raised). Sluggish, maybe ...
source to share