多线程压力测试例子 - ExecutorService shutdown() awaitTermination()

    public void testCreate() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("hisenyuan").build();
        ExecutorService pool = new ThreadPoolExecutor(
                20,
                50,
                10000L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(10240),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 50; i++) {
            // 需要提交的内容
            pool.execute(this::createTokenTest);
        }
        pool.shutdown();
        try {
            while (!pool.awaitTermination(500, TimeUnit.MILLISECONDS)) {
                LOGGER.debug("Waiting for terminate");
            }
        } catch (InterruptedException e) {
            LOGGER.error(e);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容