FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LiveLessons/SearchTaskGang/src/TaskGang.java at master · divsys/LiveLessons · GitHub
divsys
/
LiveLessons
Public
forked from
douglascraigschmidt/LiveLessons
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LiveLessons
/
SearchTaskGang
/
src
/
TaskGang.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (153 loc) · 4.93 KB
Breadcrumbs
LiveLessons
/
SearchTaskGang
/
src
/
TaskGang.java
Copy path
File metadata and controls
175 lines (153 loc) · 4.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
.
List
;
import
java
.
util
.
concurrent
.
Executor
;
import
java
.
util
.
concurrent
.
atomic
.
AtomicLong
;
/**
* @class TaskGang
*
* @brief Defines a framework for spawning and running a "gang" of
* tasks that concurrently process input from a generic List of
* elements E for one or more iteration cycles.
*/
public
abstract
class
TaskGang
<
E
>
implements
Runnable
{
/**
* The input List that's processed, which can be initialized via
* the @code makeInputList() factory method.
*/
private
volatile
List
<
E
>
mInput
=
null
;
/**
* Executes submitted Runnable tasks in a Thread pool.
*/
private
Executor
mExecutor
=
null
;
/**
* Keeps track of which cycle is currently active.
*/
private
final
AtomicLong
mCurrentCycle
=
new
AtomicLong
(
0
);
/**
* Get the List to use as input.
*/
protected
List
<
E
>
getInput
() {
return
mInput
;
}
/**
* Set the List to use as input and also return it.
*/
protected
List
<
E
>
setInput
(
List
<
E
>
input
) {
return
mInput
=
input
;
}
/**
* Set the Executor to use to submit/run tasks.
*/
protected
void
setExecutor
(
Executor
executor
) {
mExecutor
=
executor
;
}
/**
* Get the Executor to use to submit/run tasks.
*/
protected
Executor
getExecutor
() {
return
mExecutor
;
}
/**
* Increment to the next cycle.
*/
protected
long
incrementCycle
() {
return
mCurrentCycle
.
incrementAndGet
();
}
/**
* Return the current cycle.
*/
protected
long
currentCycle
() {
return
mCurrentCycle
.
get
();
}
/**
* Factory method that makes the next List of input to be
* processed concurrently by the gang of Tasks.
*/
protected
abstract
List
<
E
>
getNextInput
();
/**
* Hook method called back by initiateTaskGang() to enable
* subclasses to perform custom initializations before the tasks
* in the gang are spawned.
*/
protected
void
initiateHook
(
int
inputSize
) {
// No-op by default.
}
/**
* Initiate the TaskGang.
*/
protected
abstract
void
initiateTaskGang
(
int
inputSize
);
/**
* Hook method that returns true as long as the task processing
* should continue. By default, returns false, which means a
* TaskGang will be only "one-shot" unless this method is
* overridden.
*/
protected
boolean
advanceTaskToNextCycle
() {
return
false
;
}
/**
* Hook method that can be used as an exit barrier to wait for the
* gang of tasks to exit.
*/
protected
abstract
void
awaitTasksDone
();
/**
* Hook method called when a task is done. Can be used in
* conjunction with a one-shop or cyclic barrier to wait for all
* the other tasks to complete their current cycle. It's passed
* the index of the work that's done. Returns true if the wait
* was successfully or throws the IndexOutOfBoundsException if the
* item has been removed.
*/
protected
void
taskDone
(
int
index
)
throws
IndexOutOfBoundsException
{
// No-op.
}
/**
* Hook method that performs work a background task. Returns true
* if all goes well, else false (which will stop the background
* task from continuing to run).
*/
protected
abstract
boolean
processInput
(
E
inputData
);
/**
* Template method that creates/executes all the tasks in the
* gang.
*/
@
Override
public
void
run
() {
// Invoke hook method to get initial List of input data to
// process.
if
(
setInput
(
getNextInput
()) !=
null
) {
// Invoke hook method to initialize the gang of tasks.
initiateTaskGang
(
getInput
().
size
());
// Invoke hook method to wait for all the tasks to exit.
awaitTasksDone
();
}
}
/**
* Factory method that creates a Runnable task that will process
* one node of the input List (at location @code index) in a
* background task provided by the Executor.
*/
protected
Runnable
makeTask
(
final
int
index
) {
return
new
Runnable
() {
// This method runs in background task provided by the
// Executor.
public
void
run
() {
try
{
// Get the input data element associated with
// this index.
E
element
=
getInput
().
get
(
index
);
// Process input data element.
if
(
processInput
(
element
))
// Success indicates the worker task is done
// with this cycle.
taskDone
(
index
);
else
// A problem occurred, so return.
return
;
}
catch
(
IndexOutOfBoundsException
e
) {
return
;
}
}
};
}
}
Back
|
FazBrowse Home
|
New Git URL