What is the best way to limit the number of threads executing a particular method?


  • 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)


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

4 answers


looks the way you want.

You probably want to add some logic to use Semaphore.tryAcquire

and return an error to the user if it cannot get permission. If you use the blocking method acquire

, then you will shut down the blocked server anyway.



Take a look at Java ThreadPoolExecutor This will help you with what you are trying to do.

Hope it helps ...



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. */
  } finally {
  /* Respond with "Too busy; try later," message. */




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 ...



All Articles