Question: I have long running code, that gets battered with requests, it causes higher resource usage and unneeded concurrency problems. My solution is to have like a waiting area, where each thread waits for some predefined time. If there is no new request while the thread is waiting, it goes ahead with the operation. Any new request will reset the clock again and we release the previous thread.

I have used Semaphores and countdown latches before, but neither of them work for this particular scenario. Before I hand code this, I wanted to see if there is a standard way of doing it.

Answer: 
> 
> I have long running code, that gets battered with requests, it causes higher resource usage and unneeded concurrency problems. 
> 
> 
> 

Sounds to me that you should be using a throttled `ExecutorService`. You should have a fixed number of threads and *not* let new threads be created whenever a new request comes in. Then you can maximize you throughput by tuning the number of threads in the pool.

```
// only allow 10 concurrent requests
ExecutorService threadPool = Executors.newFixedThreadPool(10);
...
while (requestsComeIn) {
    threadPool.submit(yourRunnableRequest);
}
// you need to shut the pool down once no more requests come in
threadPool.shutdown();

```

To throttle the requests, you should use a `RejectedExecutionHandler`. Something like the following code should work which blocks after 100 elements are in the queue:

```
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100);
ThreadPoolExecutor threadPool =
      new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, queue);
// we need our RejectedExecutionHandler to block if the queue is full
threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
       @Override
       public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
           try {
                // this will block the producer until there's room in the queue
                executor.getQueue().put(r);
           } catch (InterruptedException e) {
                throw new RejectedExecutionException(
                   "Unexpected InterruptedException", e);
           }
    }
});

```

> 
> My solution is to have like a waiting area, where each thread waits for some predefined time. 
> 
> 
> 

You get this for free with the `ThreadPoolExecutor`. For example, you could allocate 1 core threads and 10 max threads and then specify (for example) `5L, TimeUnit.MINUTES` so if one of the 5 extra threads is dormant for 5 minutes it will get shutdown. It is important to note that, unfortunately, the `ThreadPoolExecutor` will not start more than the core threads *unless* the queue is full. So only after 100 things are in the queue does the 2nd thread get allocated. For that reason I usually make the core and max thread params to be the same value.
The tidiest way of keeping control is to use a queue. The modern `BlockingQueue` classes do an excellent job. You can then throttle by varying the length of the queue.

Instead of actually doing the work yourself you post a `Job` into the queue and have one or more threads sitting at the other end of the queue doing all of the work.

There is actually a structure ready-made for implementing this exact architecture, It is called an `ExecutorService`. There are many examples of use elsewhere.

See [ExecutorService](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) for details.