From ac00ef9c5800eae729c11f3f3d3407a443a2a07b Mon Sep 17 00:00:00 2001 From: GMerge Date: Thu, 29 Feb 2024 21:49:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=20=E4=BC=98=E5=8C=96ThreadP?= =?UTF-8?q?ool=E5=8D=95=E4=BE=8B=E7=9A=84=E6=80=A7=E8=83=BD=20(#404)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hyperceiler/utils/ThreadPoolManager.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/sevtinge/hyperceiler/utils/ThreadPoolManager.java b/app/src/main/java/com/sevtinge/hyperceiler/utils/ThreadPoolManager.java index 9595d43722..b41f1c2a66 100644 --- a/app/src/main/java/com/sevtinge/hyperceiler/utils/ThreadPoolManager.java +++ b/app/src/main/java/com/sevtinge/hyperceiler/utils/ThreadPoolManager.java @@ -5,17 +5,17 @@ public class ThreadPoolManager { private static final int NUM_THREADS = 5; // 定义线程池中线程的数量 - private static ExecutorService executor; + private static volatile ExecutorService executor; // 获取线程池实例 - public static synchronized ExecutorService getInstance() { - if (executor == null) { - // 创建一个具有固定数量线程的线程池 - executor = Executors.newFixedThreadPool(NUM_THREADS); - } - if (executor.isShutdown()) { - // 如果已经关机则重新创建 - executor = Executors.newFixedThreadPool(NUM_THREADS); + public static ExecutorService getInstance() { + if (executor == null || executor.isShutdown()) { + synchronized (ThreadPoolManager.class) { + if (executor == null || executor.isShutdown()) { + // 创建一个具有固定数量线程的线程池, 如果已经关机则重新创建 + executor = Executors.newFixedThreadPool(NUM_THREADS); + } + } } return executor; }