forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchTaskGangCommon.java
More file actions
110 lines (98 loc) · 3.5 KB
/
SearchTaskGangCommon.java
File metadata and controls
110 lines (98 loc) · 3.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
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import javax.naming.directory.SearchResult;
/**
* @class SearchTaskGangCommon
*
* @brief This helper class factors out the common code used by all
* the implementations of TaskGang below. It customizes the
* TaskGang framework to concurrently search an array of
* Strings for an array of words to find.
*/
public abstract class SearchTaskGangCommon
extends TaskGang<String> {
/**
* The array of words to find.
*/
protected final String[] mWordsToFind;
/**
* An Iterator for the array of Strings to search.
*/
private final Iterator<String[]> mInputIterator;
/**
* Constructor initializes the data members.
*/
protected SearchTaskGangCommon(String[] wordsToFind,
String[][] stringsToSearch) {
// Store the words to search for.
mWordsToFind = wordsToFind;
// Create an Iterator for the array of Strings to search.
mInputIterator = Arrays.asList(stringsToSearch).iterator();
}
/**
* Factory method that returns the next List of Strings to be
* searched concurrently by the TaskGang.
*/
@Override
protected List<String> getNextInput() {
if (mInputIterator.hasNext()) {
// Note that we're starting a new cycle.
incrementCycle();
// Return a List containing the Strings to search
// concurrently.
return Arrays.asList(mInputIterator.next());
}
else
// Indicate that we're done.
return null;
}
/**
* Search for all instances of @code word in @code inputData
* and return a list of all the @code SearchData results (if
* any).
*/
protected SearchResults searchForWord(String word,
String inputData) {
SearchResults results =
new SearchResults(Thread.currentThread().getId(),
currentCycle(),
word,
inputData);
// Check to see how many times (if any) the word appears
// in the input data.
for (int i = inputData.indexOf(word, 0);
i != -1;
i = inputData.indexOf(word, i + word.length())) {
// Each time a match is found it's added to the list
// of search results.
results.add(i);
}
return results;
}
/**
* Hook method that can be used as an exit barrier to wait for the
* gang of tasks to exit.
*/
protected void awaitTasksDone() {
// Only call the shutdown() and awaitTermination() methods if
// we've actually got an ExecutorService (as opposed to just
// an Executor).
if (getExecutor() instanceof ExecutorService) {
ExecutorService executorService =
(ExecutorService) getExecutor();
// Tell the ExecutorService to initiate a graceful
// shutdown.
executorService.shutdown();
try {
// Wait for all the tasks/threads in the pool to
// complete.
executorService.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
}
}
}
}