6767 * @param <W> Work -- type of work item
6868 */
6969public class WorkPool <K , W > {
70-
71- /** protecting <code>ready</code>, <code>inProgress</code> and <code>pool</code> */
72- private final Object monitor = new Object ();
73- /** An injective queue of <i>ready</i> clients. */
74- private final SetQueue <K > ready = new SetQueue <K >();
75- /** The set of clients which have work <i>in progress</i>. */
76- private final Set <K > inProgress = new HashSet <K >();
77- /** The pool of registered clients, with their work queues. */
78- private final Map <K , LinkedList <W >> pool = new HashMap <K , LinkedList <W >>();
70+ /** An injective queue of <i>ready</i> clients. */
71+ private final SetQueue <K > ready = new SetQueue <K >();
72+ /** The set of clients which have work <i>in progress</i>. */
73+ private final Set <K > inProgress = new HashSet <K >();
74+ /** The pool of registered clients, with their work queues. */
75+ private final Map <K , LinkedList <W >> pool = new HashMap <K , LinkedList <W >>();
7976
8077 /**
8178 * Add client <code><b>key</b></code> to pool of item queues, with an empty queue.
@@ -85,7 +82,7 @@ public class WorkPool<K, W> {
8582 * @param key client to add to pool
8683 */
8784 public void registerKey (K key ) {
88- synchronized (this . monitor ) {
85+ synchronized (this ) {
8986 if (!this .pool .containsKey (key )) {
9087 this .pool .put (key , new LinkedList <W >());
9188 }
@@ -97,7 +94,7 @@ public void registerKey(K key) {
9794 * @param key of client to unregister
9895 */
9996 public void unregisterKey (K key ) {
100- synchronized (this . monitor ) {
97+ synchronized (this ) {
10198 this .pool .remove (key );
10299 this .ready .remove (key );
103100 this .inProgress .remove (key );
@@ -108,7 +105,7 @@ public void unregisterKey(K key) {
108105 * Remove all clients from pool and from any other state.
109106 */
110107 public void unregisterAllKeys () {
111- synchronized (this . monitor ) {
108+ synchronized (this ) {
112109 this .pool .clear ();
113110 this .ready .clear ();
114111 this .inProgress .clear ();
@@ -126,7 +123,7 @@ public void unregisterAllKeys() {
126123 * @return key of client to whom items belong, or <code><b>null</b></code> if there is none.
127124 */
128125 public K nextWorkBlock (Collection <W > to , int size ) {
129- synchronized (this . monitor ) {
126+ synchronized (this ) {
130127 K nextKey = readyToInProgress ();
131128 if (nextKey != null ) {
132129 LinkedList <W > queue = this .pool .get (nextKey );
@@ -166,7 +163,7 @@ private static <W> int drainTo(LinkedList<W> deList, Collection<W> c, int maxEle
166163 * — <i>as a result of this work item</i>
167164 */
168165 public boolean addWorkItem (K key , W item ) {
169- synchronized (this . monitor ) {
166+ synchronized (this ) {
170167 Queue <W > queue = this .pool .get (key );
171168 if (queue != null ) {
172169 queue .offer (item );
@@ -187,7 +184,7 @@ public boolean addWorkItem(K key, W item) {
187184 * @throws IllegalStateException if registered client not <i>in progress</i>
188185 */
189186 public boolean finishWorkBlock (K key ) {
190- synchronized (this . monitor ) {
187+ synchronized (this ) {
191188 if (!this .isRegistered (key ))
192189 return false ;
193190 if (!this .inProgress .contains (key )) {
@@ -206,7 +203,7 @@ public boolean finishWorkBlock(K key) {
206203
207204 private boolean moreWorkItems (K key ) {
208205 LinkedList <W > leList = this .pool .get (key );
209- return ( leList == null ? false : !leList .isEmpty () );
206+ return leList != null && !leList .isEmpty ();
210207 }
211208
212209 /* State identification functions */
@@ -216,9 +213,9 @@ private boolean moreWorkItems(K key) {
216213 private boolean isDormant (K key ){ return !isInProgress (key ) && !isReady (key ) && isRegistered (key ); }
217214
218215 /* State transition methods - all assume key registered */
219- private void inProgressToReady (K key ){ this .inProgress .remove (key ); this .ready .addIfNotPresent (key ); };
220- private void inProgressToDormant (K key ){ this .inProgress .remove (key ); };
221- private void dormantToReady (K key ){ this .ready .addIfNotPresent (key ); };
216+ private void inProgressToReady (K key ){ this .inProgress .remove (key ); this .ready .addIfNotPresent (key ); }
217+ private void inProgressToDormant (K key ){ this .inProgress .remove (key ); }
218+ private void dormantToReady (K key ){ this .ready .addIfNotPresent (key ); }
222219
223220 /* Basic work selector and state transition step */
224221 private K readyToInProgress () {
@@ -227,5 +224,5 @@ private K readyToInProgress() {
227224 this .inProgress .add (key );
228225 }
229226 return key ;
230- };
227+ }
231228}
0 commit comments