forked from guangxush/wheel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedRunnableQueue.java
More file actions
57 lines (47 loc) · 1.42 KB
/
Copy pathLinkedRunnableQueue.java
File metadata and controls
57 lines (47 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.LinkedList;
/**
* @author: guangxush
* @create: 2019/08/24
*/
public class LinkedRunnableQueue implements RunnableQueue {
private final int limit;
private final DenyPolicy denyPolicy;
private final LinkedList<Runnable> runnableList = new LinkedList<>();
private final ThreadPool threadPool;
public LinkedRunnableQueue(int limit, DenyPolicy denyPolicy, ThreadPool threadPool) {
this.limit = limit;
this.denyPolicy = denyPolicy;
this.threadPool = threadPool;
}
@Override
public void offer(Runnable runnable) {
synchronized (runnableList) {
if (runnableList.size() >= limit) {
denyPolicy.reject(runnable, threadPool);
} else {
runnableList.add(runnable);
runnableList.notifyAll();
}
}
}
@Override
public Runnable take() throws InterruptedException {
synchronized (runnableList) {
while (runnableList.isEmpty()) {
try {
//没有任务将当前线程挂起
runnableList.wait();
} catch (InterruptedException e) {
throw e;
}
}
}
return runnableList.removeFirst();
}
@Override
public int size() {
synchronized (runnableList){
return runnableList.size();
}
}
}