forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadJoinTest.java
More file actions
176 lines (155 loc) · 6.13 KB
/
ThreadJoinTest.java
File metadata and controls
176 lines (155 loc) · 6.13 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
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
/**
* @class ThreadJoinTest
*
* @brief Demonstrates the use of Java Thread.join() as a simple
* barrier synchronizer to implement an "embarrassingly
* parallel" application that concurrently searches for words
* in a List of Strings.
*/
public class ThreadJoinTest {
/**
* If this is set to true then lots of debugging output will be
* generated.
*/
public static boolean diagnosticsEnabled = true;
/**
* This input array is used by the ThreadJoinTest to search for
* the words concurrently in multiple threads.
*/
private final static String[] mOneShotInputStrings =
{"xreo", "xfao", "xmiomio", "xlao", "xtiotio", "xsoosoo", "xdoo", "xdoodoo"};
// List of words to search for.
private static String[] mWordList = {"do",
"re",
"mi",
"fa",
"so",
"la",
"ti",
"do"};
/**
* @class SearchOneShotThreadGangJoin
*
* @brief Starts a Thread for each element in the List of input
* Strings and uses Thread.join() to wait for all the
* Threads to finish. This implementation doesn't require
* any Java synchronization mechanisms other than what's
* provided by Thread.
*/
static public class SearchOneShotThreadJoin {
/**
* The input List that's processed.
*/
private volatile List<String> mInput = null;
/**
* The array of words to find.
*/
final String[] mWordsToFind;
/**
* The List of worker Threads that were created.
*/
private List<Thread> mWorkerThreads;
/**
* Constructor initializes the data members.
*/
public SearchOneShotThreadJoin(String[] wordsToFind,
String[] inputStrings) {
// Initialize the data members.
mWordsToFind = wordsToFind;
mInput = Arrays.asList(inputStrings);
// This List holds Threads so they can be joined when their processing is done.
mWorkerThreads = new LinkedList<Thread>();
// Create and start a Thread for each element in the
// mInput.
for (int i = 0; i < mInput.size(); ++i) {
// Each Thread performs the processing designated by
// the processInput() method of the worker Runnable.
Thread t = new Thread(makeTask(i));
// Add to the List of Threads to join.
mWorkerThreads.add(t);
// Start the Thread to process its input in the background.
t.start();
}
// Barrier synchronization.
for (Thread thread : mWorkerThreads)
try {
thread.join();
} catch (InterruptedException e) {
printDebugging("join() interrupted");
}
}
/**
* Factory method that creates a Runnable task that will
* process one node of the input List (at location @code
* index) in a background Thread .
*/
private Runnable makeTask(final int index) {
return new Runnable() {
// This method runs in background Thread.
public void run() {
// Get the input data element associated with
// this index.
String element = mInput.get(index);
// Process input data element.
if (processInput(element) == false)
return;
}
};
}
/**
* Run in a background Thread and search the inputData for
* all occurrences of the words to find. Each time a match is
* found the processResults() hook method is called to handle
* the results.
*/
private boolean processInput (String inputData) {
// Iterate through each word we're searching for.
for (String word : mWordsToFind)
// 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 the processResults()
// hook method is called to handle the results.
processResults("in thread "
+ Thread.currentThread().getId()
+ " "
+ word
+ " was found at offset "
+ i
+ " in string "
+ inputData);
return true;
}
/**
* Hook method that processes the results.
*/
private void processResults(String results) {
printDebugging(results);
}
}
/**
* Print debugging output if @code diagnosticsEnabled is true.
*/
static void printDebugging(String output) {
if (diagnosticsEnabled)
System.out.println(output);
}
/**
* This is the entry point into the test program.
*/
static public void main(String[] args) {
printDebugging("Starting ThreadJoinTest");
// Create/run appropriate type of SearchThreadGang to search
// for words concurrently.
printDebugging("Starting JOIN");
new SearchOneShotThreadJoin(mWordList,
mOneShotInputStrings);
printDebugging("Ending JOIN");
printDebugging("Ending ThreadJoinTest");
}
}