107 |
|
* <p><b>Sample usages:</b> |
108 |
|
* |
109 |
|
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
110 |
< |
* to control a one-shot action serving a variable number of |
111 |
< |
* parties. The typical idiom is for the method setting this up to |
112 |
< |
* first register, then start the actions, then deregister, as in: |
110 |
> |
* to control a one-shot action serving a variable number of parties. |
111 |
> |
* The typical idiom is for the method setting this up to first |
112 |
> |
* register, then start the actions, then deregister, as in: |
113 |
|
* |
114 |
|
* <pre> {@code |
115 |
|
* void runTasks(List<Runnable> tasks) { |
182 |
|
* <p>To create a set of tasks using a tree of phasers, |
183 |
|
* you could use code of the following form, assuming a |
184 |
|
* Task class with a constructor accepting a phaser that |
185 |
< |
* it registers for upon construction: |
185 |
> |
* it registers with upon construction: |
186 |
|
* |
187 |
|
* <pre> {@code |
188 |
|
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
205 |
|
* be appropriate for extremely small per-barrier task bodies (thus |
206 |
|
* high rates), or up to hundreds for extremely large ones. |
207 |
|
* |
208 |
– |
* </pre> |
209 |
– |
* |
208 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
209 |
|
* maximum number of parties to 65535. Attempts to register additional |
210 |
|
* parties result in {@code IllegalStateException}. However, you can and |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* Creates a new phaser with the given numbers of registered |
347 |
> |
* Creates a new phaser with the given number of registered |
348 |
|
* unarrived parties, initial phase number 0, and no parent. |
349 |
|
* |
350 |
|
* @param parties the number of parties required to trip barrier |
376 |
|
} |
377 |
|
|
378 |
|
/** |
379 |
< |
* Creates a new phaser with the given parent and numbers of |
379 |
> |
* Creates a new phaser with the given parent and number of |
380 |
|
* registered unarrived parties. If parent is non-null, this phaser |
381 |
|
* is registered with the parent and its initial phase number is |
382 |
|
* the same as that of parent phaser. |
414 |
|
/** |
415 |
|
* Adds the given number of new unarrived parties to this phaser. |
416 |
|
* |
417 |
< |
* @param parties the number of parties required to trip barrier |
417 |
> |
* @param parties the number of additional parties required to trip barrier |
418 |
|
* @return the arrival phase number to which this registration applied |
419 |
|
* @throws IllegalStateException if attempting to register more |
420 |
|
* than the maximum supported number of parties |
421 |
+ |
* @throws IllegalArgumentException if {@code parties < 0) |
422 |
|
*/ |
423 |
|
public int bulkRegister(int parties) { |
424 |
|
if (parties < 0) |
560 |
|
* Arrives at the barrier and awaits others. Equivalent in effect |
561 |
|
* to {@code awaitAdvance(arrive())}. If you need to await with |
562 |
|
* interruption or timeout, you can arrange this with an analogous |
563 |
< |
* construction using one of the other forms of the awaitAdvance |
564 |
< |
* method. If instead you need to deregister upon arrival use |
565 |
< |
* {@code arriveAndDeregister}. It is an unenforced usage error |
566 |
< |
* for an unregistered party to invoke this method. |
563 |
> |
* construction using one of the other forms of the {@code |
564 |
> |
* awaitAdvance} method. If instead you need to deregister upon |
565 |
> |
* arrival, use {@link #arriveAndDeregister}. It is an unenforced |
566 |
> |
* usage error for an unregistered party to invoke this method. |
567 |
|
* |
568 |
|
* @return the arrival phase number, or a negative number if terminated |
569 |
|
* @throws IllegalStateException if not terminated and the number |
826 |
|
volatile boolean wasInterrupted = false; |
827 |
|
volatile Thread thread; // nulled to cancel wait |
828 |
|
QNode next; |
829 |
+ |
|
830 |
|
QNode(Phaser phaser, int phase, boolean interruptible, |
831 |
|
boolean timed, long startTime, long nanos) { |
832 |
|
this.phaser = phaser; |
837 |
|
this.nanos = nanos; |
838 |
|
thread = Thread.currentThread(); |
839 |
|
} |
840 |
+ |
|
841 |
|
public boolean isReleasable() { |
842 |
|
return (thread == null || |
843 |
|
phaser.getPhase() != phase || |
844 |
|
(interruptible && wasInterrupted) || |
845 |
|
(timed && (nanos - (System.nanoTime() - startTime)) <= 0)); |
846 |
|
} |
847 |
+ |
|
848 |
|
public boolean block() { |
849 |
|
if (Thread.interrupted()) { |
850 |
|
wasInterrupted = true; |
861 |
|
} |
862 |
|
return isReleasable(); |
863 |
|
} |
864 |
+ |
|
865 |
|
void signal() { |
866 |
|
Thread t = thread; |
867 |
|
if (t != null) { |
869 |
|
LockSupport.unpark(t); |
870 |
|
} |
871 |
|
} |
872 |
+ |
|
873 |
|
boolean doWait() { |
874 |
|
if (thread != null) { |
875 |
|
try { |
879 |
|
} |
880 |
|
return wasInterrupted; |
881 |
|
} |
878 |
– |
|
882 |
|
} |
883 |
|
|
884 |
|
/** |