forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneShotExecutorService.java
More file actions
218 lines (191 loc) · 7.22 KB
/
OneShotExecutorService.java
File metadata and controls
218 lines (191 loc) · 7.22 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
/**
* @class OneShotExecutorService
*
* @brief Customizes the SearchTaskGangCommon framework to process a
* one-shot List of tasks via a fixed-size pool of Threads
* created by the ExecutorService, which is also used as a
* barrier synchronizer to wait for all the Threads in the pool
* to shutdown. The unit of concurrency is invokeAll(), which
* creates a task for each input string. The results
* processing model uses a LinkedBlockingQueue that stores
* results for immediate concurrent processing per cycle.
*/
public class OneShotExecutorService
extends SearchTaskGangCommon {
/**
* Controls when the framework exits.
*/
protected CountDownLatch mExitBarrier = null;
/**
* Queue to store SearchResults of concurrent computations.
*/
private BlockingQueue<SearchResults> mResultsQueue =
new LinkedBlockingQueue<SearchResults>();
/**
* Number of Threads in the pool.
*/
protected final int MAX_THREADS = 4;
/**
* Constructor initializes the superclass.
*/
public OneShotExecutorService(String[] wordsToFind,
String[][] stringsToSearch) {
// Pass input to superclass constructor.
super(wordsToFind,
stringsToSearch);
}
/**
* Hook method that initiates the gang of Threads by using a
* fixed-size Thread pool managed by the ExecutorService.
*/
@Override
protected void initiateHook(int inputSize) {
System.out.println("@@@ starting cycle "
+ currentCycle()
+ " with "
+ inputSize
+ " tasks@@@");
// Initialize the exit barrier to inputSize, which causes
// awaitTasksDone() to block until the cycle is finished.
mExitBarrier = new CountDownLatch(inputSize);
// Create a fixed-size Thread pool.
if (getExecutor() == null)
setExecutor (Executors.newFixedThreadPool(MAX_THREADS));
}
/**
* Initiate the TaskGang to run each worker in the Thread pool.
*/
protected void initiateTaskGang(int inputSize) {
// Allow subclasses to customize their behavior before the
// Threads in the gang are spawned.
initiateHook(inputSize);
// Create a new collection that will contain all the
// Worker Runnables.
List<Callable<Object>> workerCollection =
new ArrayList<Callable<Object>>(inputSize);
// Create a Runnable for each item in the input List and add
// it as a Callable adapter into the collection.
for (int i = 0; i < inputSize; ++i)
workerCollection.add(Executors.callable(makeTask(i)));
try {
// Downcast to get the ExecutorService.
ExecutorService executorService =
(ExecutorService) getExecutor();
// Use invokeAll() to execute all items in the collection
// via the Executor's Thread pool.
executorService.invokeAll(workerCollection);
} catch (InterruptedException e) {
System.out.println("invokeAll() interrupted");
}
}
/**
* Runs as a background task and searches inputData for all
* occurrences of the words to find.
*/
@Override
protected boolean processInput (String inputData) {
// Iterate through each word we're searching for
// and try to find it in the inputData.
for (String word : mWordsToFind)
// Each time a match is found the queueResults() method is
// called to pass the search results to a background
// Thread for concurrent processing.
queueResults(searchForWord(word,
inputData));
return true;
}
/**
* Hook method called when a worker task is done.
*/
@Override
protected void taskDone(int index) throws IndexOutOfBoundsException {
// Decrements the CountDownLatch, which releases the main
// Thread when count drops to 0.
mExitBarrier.countDown();
}
/**
* Used by processInput() to queue results for processing in a
* background Thread.
*/
protected void queueResults(SearchResults results) {
getQueue().add(results);
}
/**
* Hook method that shuts down the ExecutorService's Thread pool
* and waits for all the tasks to exit before returning.
*/
@Override
protected void awaitTasksDone() {
do {
// Start processing this cycle of results concurrently in
// a background Thread.
processQueuedResults(getInput().size()
* mWordsToFind.length);
try {
// Wait until the exit barrier has been tripped.
mExitBarrier.await();
} catch (InterruptedException e) {
System.out.println("await() interrupted");
}
// Keep looping as long as there's another cycle's
// worth of input.
} while (advanceTaskToNextCycle());
// Call up to the super class to await for ExecutorService's
// Threads to shutdown.
super.awaitTasksDone();
}
/**
* Set the BlockingQueue and return the existing queue.
*/
protected BlockingQueue<SearchResults> setQueue(BlockingQueue<SearchResults> q) {
BlockingQueue<SearchResults> old = mResultsQueue;
mResultsQueue = q;
return old;
}
/**
* Get the BlockingQueue.
*/
protected BlockingQueue<SearchResults> getQueue() {
return mResultsQueue;
}
/**
* Process all the queued results concurrently in a background
* Thread.
*/
protected void processQueuedResults(final int resultCount) {
// This runnable processes all queued results.
Runnable processQueuedResultsRunnable =
new Runnable() {
public void run() {
try {
for (int i = 0; i < resultCount; ++i)
// Extract each SearchResults from the
// queue (blocking if necessary) until
// we're done.
getQueue().take().print();
} catch (InterruptedException e) {
System.out.println("run() interrupted");
}
}
};
// Create a new Thread that will process the results
// concurrently in the background.
Thread t = new Thread(processQueuedResultsRunnable);
t.start();
try {
// Simple barrier that waits for the Thread processing
// SearchResults to exit.
t.join();
} catch (InterruptedException e) {
System.out.println("processQueuedResults() interrupted");
}
}
}