Repository navigation
Expand file tree
/
Copy pathExecutorPipeline.java
More file actions
129 lines (113 loc) · 4.5 KB
/
Copy pathExecutorPipeline.java
File metadata and controls
129 lines (113 loc) · 4.5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Function;
public class ExecutorPipeline<In, Out> {
private BlockingQueue<Runnable> queue;
private ThreadPoolExecutor exce;
private boolean preserveOrder = false;
private Function<In, Out> processor;
private ExecutorPipeline<Out, ?> next;
private Consumer<Throwable> exceptionHandler;
private String threadPoolName;
private AtomicLong orderCounter = new AtomicLong();
private ReentrantLock lock = new ReentrantLock();
private long nextOutTaskNumber = 0;
private Map<Long, Out> orderMap = new HashMap<>();
private static AtomicInteger pipeNumber = new AtomicInteger(1);
private AtomicInteger threadNumber = new AtomicInteger(1);
public ExecutorPipeline(int threads, int queueSize, boolean preserveOrder, Function<In, Out> processor,
Consumer<Throwable> exceptionHandler) {
queue = new LimitedQueue<>(queueSize);
exce = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, queue, r -> new Thread(r, threadPoolName + "-" + threadNumber.getAndIncrement()));
this.preserveOrder = preserveOrder;
this.processor = processor;
this.exceptionHandler = exceptionHandler;
this.threadPoolName = "pipe-" + pipeNumber.getAndIncrement();
}
private static class LimitedQueue<E> extends LinkedBlockingQueue<E> {
public LimitedQueue(int maxSize) {
super(maxSize);
}
@Override
public boolean offer(E e) {
// turn offer() and add() into a blocking calls (unless interrupted)
try {
put(e);
return true;
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
return false;
}
}
public ExecutorPipeline<Out, Void> add(int threads, int queueSize, final Consumer<Out> consumer) {
return add(threads, queueSize, false, out -> {
consumer.accept(out);
return null;
});
}
public <NextOut> ExecutorPipeline<Out, NextOut> add(int threads, int queueSize, boolean preserveOrder,
Function<Out, NextOut> processor) {
ExecutorPipeline<Out, NextOut> ret = new ExecutorPipeline<>(threads, queueSize, preserveOrder, processor,
exceptionHandler);
next = ret;
return ret;
}
private void pushNext(long order, Out res) {
if (next != null) {
if (!preserveOrder) {
next.push(res);
} else {
lock.lock();
try {
if (order == nextOutTaskNumber) {
next.push(res);
while (true) {
nextOutTaskNumber++;
Out out = orderMap.remove(nextOutTaskNumber);
if (out == null) break;
next.push(out);
}
} else {
orderMap.put(order, res);
}
} finally {
lock.unlock();
}
}
}
}
public void push(final In in) {
final long order = orderCounter.getAndIncrement();
exce.execute(new Runnable() {
@Override
public void run() {
try {
pushNext(order, processor.apply(in));
} catch (Throwable e) {
exceptionHandler.accept(e);
}
}
});
}
}