forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskGangTest.java
More file actions
98 lines (88 loc) · 3.29 KB
/
TaskGangTest.java
File metadata and controls
98 lines (88 loc) · 3.29 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
/**
* @class TaskGangTest
*
* @brief This test driver showcases how various subclasses customize
* the TaskGang framework with different Java concurrency and
* synchronization mechanisms to implement an "embarrassingly
* parallel" application that concurrently searches for words
* in a List of input Strings.
*/
public class TaskGangTest {
/**
* Enumerate the tests to run.
*/
enum TestsToRun {
ONESHOT_THREAD_PER_TASK,
ONESHOT_EXECUTOR_SERVICE,
ONESHOT_EXECUTOR_SERVICE_FUTURE,
ONESHOT_EXECUTOR_COMPLETION_SERVICE
}
/**
* Array of words to search for in the input.
*/
private final static String[] mWordList = {"do",
"re",
"mi",
"fa",
"so",
"la",
"ti",
"do"};
/**
* This input array is used by the one-shot tests that search for
* the words concurrently in multiple threads.
*/
private final static String[][] mOneShotInputStrings = {
{"xreo", "xfao", "xmiomio", "xlao", "xtiotio", "xsoosoo", "xdoo", "xdoodoo"}
};
/**
* If this is set to true then lots of debugging output will be
* generated.
*/
public static boolean diagnosticsEnabled = true;
/**
* Print debugging output if @code diagnosticsEnabled is true.
*/
private static void printDebugging(String output) {
if (diagnosticsEnabled)
System.out.println(output);
}
/**
* Factory method that creates the desired type of TaskGang
* subclass implementation.
*/
private static Runnable makeTaskGang(String[] wordList,
TestsToRun choice) {
switch(choice) {
case ONESHOT_THREAD_PER_TASK:
return new OneShotThreadPerTask(wordList,
mOneShotInputStrings);
case ONESHOT_EXECUTOR_SERVICE:
return new OneShotExecutorService(wordList,
mOneShotInputStrings);
case ONESHOT_EXECUTOR_SERVICE_FUTURE:
return new OneShotExecutorServiceFuture(wordList,
mOneShotInputStrings);
case ONESHOT_EXECUTOR_COMPLETION_SERVICE:
return new OneShotExecutorCompletionService(wordList,
mOneShotInputStrings);
}
return null;
}
/**
* This is the entry point into the test program.
*/
public static void main(String[] args) {
printDebugging("Starting TaskGangTest");
// Create/run appropriate type of SearchTaskGang to search for
// words concurrently.
for (TestsToRun test : TestsToRun.values()) {
printDebugging("Starting "
+ test);
makeTaskGang(mWordList, test).run();
printDebugging("Ending "
+ test);
}
printDebugging("Ending TaskGangTest");
}
}