Skip to content

Instantly share code, notes, and snippets.

@tylertreat-wf
Created November 28, 2016 16:46
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 tylertreat-wf/3de044a9610f06ed67919260df1724ad to your computer and use it in GitHub Desktop.
Save tylertreat-wf/3de044a9610f06ed67919260df1724ad to your computer and use it in GitHub Desktop.
thread pool test
import java.util.concurrent.*;
public class ThreadPoolTest {
public static void main(String[] args) {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(64);
ExecutorService threadPool = new LoggingThreadPoolExecutor(1, 10, 30, TimeUnit.SECONDS, queue);
for (int i = 0; i < 1000; i++) {
threadPool.submit(() -> {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
threadPool.shutdown();
}
static class LoggingThreadPoolExecutor extends ThreadPoolExecutor {
public LoggingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
System.out.println("active threads: " + getActiveCount());
System.out.println("max seen threads: " + getLargestPoolSize());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment