Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:BugFix: fixed TaskManager #347

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/main/java/com/blade/task/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,6 +39,9 @@
public final class TaskManager {

private final static Map<String, Task> 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;

Expand All @@ -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<Task> getTasks() {
return new ArrayList<>(TASK_MAP.values());
Collection<Task> 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();
}

}
30 changes: 30 additions & 0 deletions src/test/java/com/blade/task/TaskManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.blade.task;

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 {

@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(() -> {
TaskManager.addTask(task);
downLatch.countDown();
}).start();
});

downLatch.await();
Assert.assertEquals(tackCount, TaskManager.getTasks().size());
}
}