From 58720493916a5b0c72edfcb135ff04d773a71191 Mon Sep 17 00:00:00 2001 From: hhf <614792109@qq.com> Date: Tue, 26 Oct 2021 18:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E9=87=8D=E6=A0=A1=E9=AA=8C=E9=94=81bu?= =?UTF-8?q?g=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lili/common/utils/ThreadPoolUtil.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/framework/src/main/java/cn/lili/common/utils/ThreadPoolUtil.java b/framework/src/main/java/cn/lili/common/utils/ThreadPoolUtil.java index 17f87182..5a62281a 100644 --- a/framework/src/main/java/cn/lili/common/utils/ThreadPoolUtil.java +++ b/framework/src/main/java/cn/lili/common/utils/ThreadPoolUtil.java @@ -24,7 +24,10 @@ public class ThreadPoolUtil { */ private static final BlockingQueue BQUEUE = new ArrayBlockingQueue(100); private static final ThreadPoolExecutor POOL = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, ALIVE_TIME, TimeUnit.MILLISECONDS, BQUEUE, new ThreadPoolExecutor.CallerRunsPolicy()); - public static ThreadPoolExecutor threadPool; + /** + * volatile禁止指令重排 + */ + public static volatile ThreadPoolExecutor threadPool; static { POOL.prestartAllCoreThreads(); @@ -49,22 +52,20 @@ public class ThreadPoolUtil { } /** - * dcs获取线程池 + * DCL获取线程池 * * @return 线程池对象 */ public static ThreadPoolExecutor getThreadPool() { if (threadPool != null) { return threadPool; - } else { - synchronized (ThreadPoolUtil.class) { - if (threadPool == null) { - threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); - return threadPool; - } - return threadPool; + } + synchronized (ThreadPoolUtil.class) { + if (threadPool == null) { + threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); } } + return threadPool; } public static ThreadPoolExecutor getPool() {