From e38adab6c1d353480046eafc0778d943c18620b0 Mon Sep 17 00:00:00 2001 From: psh686868 <1101311464@qq.com> Date: Sat, 16 Mar 2019 16:24:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:BugFix:=20fixed=20TaskManager,=20=E5=BD=93?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=9A=84=E7=BA=BF=E7=A8=8B=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=9A=84=E6=97=B6=E5=80=99=E4=BC=9A=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E4=BB=BB=E5=8A=A1=E6=B7=BB=E5=8A=A0=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/blade/task/TaskManager.java | 45 ++++++++++++++----- .../java/com/blade/task/TaskManagerTest.java | 27 +++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/blade/task/TaskManagerTest.java diff --git a/src/main/java/com/blade/task/TaskManager.java b/src/main/java/com/blade/task/TaskManager.java index 14a28719f..08d2e9157 100644 --- a/src/main/java/com/blade/task/TaskManager.java +++ b/src/main/java/com/blade/task/TaskManager.java @@ -19,12 +19,10 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import lombok.var; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import static com.blade.kit.BladeKit.getStartedSymbol; @@ -41,6 +39,9 @@ public final class TaskManager { private final static Map TASK_MAP = new HashMap<>(8); + private final static ReentrantReadWriteLock rrw = new ReentrantReadWriteLock(); + private final static Lock readLock = rrw.readLock(); + private final static Lock writeLock = rrw.writeLock(); private static CronExecutorService cronExecutorService; @@ -57,21 +58,45 @@ public static CronExecutorService getExecutorService() { } public static void addTask(Task task) { - TASK_MAP.put(task.getName(), task); + writeLock.lock(); + try { + TASK_MAP.put(task.getName(), task); + } finally { + writeLock.unlock(); + } log.info("{}Add task [{}]", getStartedSymbol(), task.getName()); } public static List getTasks() { - return new ArrayList<>(TASK_MAP.values()); + Collection values; + readLock.lock(); + try { + values = Optional.ofNullable(TASK_MAP.values()).orElse(Collections.EMPTY_LIST); + } finally { + readLock.unlock(); + } + return new ArrayList<>(values); } public static Task getTask(String name) { - return TASK_MAP.get(name); + readLock.lock(); + try { + return TASK_MAP.get(name); + } finally { + readLock.unlock(); + } + } public static boolean stopTask(String name) { - var task = TASK_MAP.get(name); - return task.stop(); + Task task; + readLock.lock(); + try { + task = TASK_MAP.get(name); + } finally { + readLock.unlock(); + } + return task == null ? Boolean.FALSE : task.stop(); } } diff --git a/src/test/java/com/blade/task/TaskManagerTest.java b/src/test/java/com/blade/task/TaskManagerTest.java new file mode 100644 index 000000000..350da7973 --- /dev/null +++ b/src/test/java/com/blade/task/TaskManagerTest.java @@ -0,0 +1,27 @@ +package com.blade.task; + +import java.util.concurrent.CountDownLatch; +import java.util.stream.IntStream; + +/** + * @author PSH + * @date 2019/03/16 + */ +public class TaskManagerTest { + public static void main(String[] args) { + CountDownLatch downLatch = new CountDownLatch(500); + IntStream.range(0,500).forEach(i -> { + Task task = new Task("task-" + i, null, Integer.MAX_VALUE); + new Thread(()->{ + TaskManager.addTask(task); + downLatch.countDown(); + }).start(); + }); + try { + downLatch.await(); + System.out.println("TaskManager.getTasks().size() = " + TaskManager.getTasks().size()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} From e937ee4a57e8aafaaf566d7dd2e624b3a963edea Mon Sep 17 00:00:00 2001 From: psh686868 Date: Mon, 18 Mar 2019 13:30:55 +0800 Subject: [PATCH 2/2] :update: update TackManager --- .../java/com/blade/task/TaskManagerTest.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/blade/task/TaskManagerTest.java b/src/test/java/com/blade/task/TaskManagerTest.java index 350da7973..5fea19d00 100644 --- a/src/test/java/com/blade/task/TaskManagerTest.java +++ b/src/test/java/com/blade/task/TaskManagerTest.java @@ -2,26 +2,29 @@ import java.util.concurrent.CountDownLatch; import java.util.stream.IntStream; +import org.junit.Assert; +import org.junit.Test; /** * @author PSH * @date 2019/03/16 */ public class TaskManagerTest { - public static void main(String[] args) { - CountDownLatch downLatch = new CountDownLatch(500); - IntStream.range(0,500).forEach(i -> { + + @Test + public void testAddTaskMultiThreading() throws Exception { + + final int tackCount = 500; + CountDownLatch downLatch = new CountDownLatch(tackCount); + IntStream.range(0, tackCount).forEach(i -> { Task task = new Task("task-" + i, null, Integer.MAX_VALUE); - new Thread(()->{ + new Thread(() -> { TaskManager.addTask(task); downLatch.countDown(); }).start(); }); - try { - downLatch.await(); - System.out.println("TaskManager.getTasks().size() = " + TaskManager.getTasks().size()); - } catch (InterruptedException e) { - e.printStackTrace(); - } + + downLatch.await(); + Assert.assertEquals(tackCount, TaskManager.getTasks().size()); } }