和朱晔一起复习Java并发(一):线程池 (12)

现在来写一段测试代码:

@Slf4j public class TomcatThreadPoolTest { @Test public void test() throws InterruptedException { TomcatTaskQueue taskqueue = new TomcatTaskQueue(5); TomcatThreadPool threadPool = new TomcatThreadPool(2, 5, 60, TimeUnit.SECONDS, taskqueue); taskqueue.setParent(threadPool); IntStream.rangeClosed(1, 10).forEach(i -> threadPool.execute(new Task(true, i))); IntStream.rangeClosed(1, 10).forEach(i -> threadPool.execute(new Task(false, i) , 1050, TimeUnit.MILLISECONDS)); threadPool.shutdown(); threadPool.awaitTermination(1, TimeUnit.HOURS); } @ToString class Task implements Runnable { private boolean slow; private String name; public Task(boolean slow, int index) { this.slow = slow; this.name = String.format("%s-%d", slow ? "slow" : "quick", index); } @Override public void run() { log.info("Start:{}", name); if (slow) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } log.info("Finish:{}", name); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgdxwx.html