Created
May 3, 2012 01:04
-
-
Save briangordon/2582308 to your computer and use it in GitHub Desktop.
A ThreadPoolExecutor that shuts down when it runs out of tasks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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