Java线程池

创建线程池

private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(channel + "-pool-%d").build();
/**
     * 线程池
     */
    private ExecutorService pool = new ThreadPoolExecutor(5,
        200,
        0L,
        TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<>(1024),
        namedThreadFactory,
        new ThreadPoolExecutor.AbortPolicy());
		
pool.execute(() -> {
	while (true) {
		try {
			// 初始化环境
			LocalMessage message = LocalMessageQueue.out(channel);
			pool.execute(() -> consumer(message));

		}
		catch (InterruptedException e) {
			e.printStackTrace();
			Thread.currentThread().interrupt();
		}

		if (false) {
			break;
		}

	}
});
posted @ 2024-10-29 14:24  fight139  阅读(4)  评论(0编辑  收藏  举报