Created
November 28, 2016 16:46
-
-
Save tylertreat-wf/3de044a9610f06ed67919260df1724ad to your computer and use it in GitHub Desktop.
thread pool test
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
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