Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f2d12f3

Browse files
thread wait & notify test
1 parent 0e5bdbf commit f2d12f3

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package thread.notify;
2+
3+
import java.util.Stack;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
import util.SimpleLogger;
6+
7+
/**
8+
* @GitHub : https://github.com/zacscoding
9+
*/
10+
public class TaskScheduler implements Runnable {
11+
12+
public static Object wakeupSyncObject = new Object();
13+
14+
private volatile boolean shouldRun = true;
15+
private ReentrantReadWriteLock lock;
16+
private Stack<String> tasks;
17+
private TaskListener listener;
18+
private Thread schedulerThread;
19+
20+
public TaskScheduler(TaskListener listener) {
21+
this.listener = listener;
22+
this.tasks = new Stack<>();
23+
this.lock = new ReentrantReadWriteLock();
24+
this.schedulerThread = new Thread(this, "TaskScheduler");
25+
this.schedulerThread.setDaemon(true);
26+
this.schedulerThread.start();
27+
}
28+
29+
public void registerTask(String taskName) {
30+
try {
31+
lock.writeLock().lock();
32+
tasks.push(taskName);
33+
awake();
34+
} finally {
35+
lock.writeLock().unlock();
36+
}
37+
}
38+
39+
public void start() {
40+
schedulerThread = new Thread(this);
41+
schedulerThread.start();
42+
}
43+
44+
public void stop() {
45+
shouldRun = false;
46+
schedulerThread.interrupt();
47+
}
48+
49+
public void awake() {
50+
synchronized (wakeupSyncObject) {
51+
wakeupSyncObject.notify();
52+
}
53+
}
54+
55+
@Override
56+
public void run() {
57+
while (shouldRun) {
58+
try {
59+
synchronized (wakeupSyncObject) {
60+
if (tasks.isEmpty()) {
61+
wakeupSyncObject.wait(3000L);
62+
}
63+
}
64+
doWork();
65+
} catch (InterruptedException e) {
66+
shouldRun = false;
67+
}
68+
}
69+
}
70+
71+
private void doWork() {
72+
SimpleLogger.info("Start task executor");
73+
try {
74+
lock.readLock().lock();
75+
if (tasks.isEmpty()) {
76+
return;
77+
}
78+
String taskName = tasks.pop();
79+
SimpleLogger.info("Do task : {}", taskName);
80+
listener.onComplete(taskName);
81+
} finally {
82+
lock.readLock().unlock();
83+
}
84+
}
85+
86+
public interface TaskListener {
87+
88+
void onComplete(String taskName);
89+
}
90+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package thread.notify;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
import org.junit.Test;
6+
import thread.notify.TaskScheduler.TaskListener;
7+
import util.SimpleLogger;
8+
9+
/**
10+
* @GitHub : https://github.com/zacscoding
11+
*/
12+
public class TaskSchedulerTest {
13+
14+
@Test
15+
public void doWorkTest() throws Exception {
16+
TaskScheduler scheduler = new TaskScheduler(new TaskListener() {
17+
@Override
18+
public void onComplete(String taskName) {
19+
20+
}
21+
});
22+
scheduler.start();
23+
TimeUnit.SECONDS.sleep(4L);
24+
SimpleLogger.info(">> Register task01, task02");
25+
scheduler.registerTask("task01");
26+
scheduler.registerTask("task02");
27+
TimeUnit.SECONDS.sleep(4L);
28+
SimpleLogger.info(">> Register task03");
29+
scheduler.stop();
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /