Skip to content

Instantly share code, notes, and snippets.

@briangordon
Created May 3, 2012 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briangordon/2582308 to your computer and use it in GitHub Desktop.
Save briangordon/2582308 to your computer and use it in GitHub Desktop.
A ThreadPoolExecutor that shuts down when it runs out of tasks.
public class RecursiveThreadPoolExecutor extends ThreadPoolExecutor {
private AtomicInteger count;
public RecursiveThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
count = new AtomicInteger(0);
}
public void execute(Runnable command) {
count.getAndIncrement();
super.execute(command);
}
protected void afterExecute(Runnable r, Throwable t) {
if(count.decrementAndGet()==0) {
this.shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment