109 |
|
* <p><b>Sample usages:</b> |
110 |
|
* |
111 |
|
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
112 |
< |
* to control a one-shot action serving a variable number of |
113 |
< |
* parties. The typical idiom is for the method setting this up to |
114 |
< |
* first register, then start the actions, then deregister, as in: |
112 |
> |
* to control a one-shot action serving a variable number of parties. |
113 |
> |
* The typical idiom is for the method setting this up to first |
114 |
> |
* register, then start the actions, then deregister, as in: |
115 |
|
* |
116 |
|
* <pre> {@code |
117 |
|
* void runTasks(List<Runnable> tasks) { |
184 |
|
* <p>To create a set of tasks using a tree of phasers, |
185 |
|
* you could use code of the following form, assuming a |
186 |
|
* Task class with a constructor accepting a phaser that |
187 |
< |
* it registers for upon construction: |
187 |
> |
* it registers with upon construction: |
188 |
|
* |
189 |
|
* <pre> {@code |
190 |
|
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
207 |
|
* be appropriate for extremely small per-barrier task bodies (thus |
208 |
|
* high rates), or up to hundreds for extremely large ones. |
209 |
|
* |
210 |
– |
* </pre> |
211 |
– |
* |
210 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
211 |
|
* maximum number of parties to 65535. Attempts to register additional |
212 |
|
* parties result in {@code IllegalStateException}. However, you can and |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* Creates a new phaser with the given numbers of registered |
349 |
> |
* Creates a new phaser with the given number of registered |
350 |
|
* unarrived parties, initial phase number 0, and no parent. |
351 |
|
* |
352 |
|
* @param parties the number of parties required to trip barrier |
378 |
|
} |
379 |
|
|
380 |
|
/** |
381 |
< |
* Creates a new phaser with the given parent and numbers of |
381 |
> |
* Creates a new phaser with the given parent and number of |
382 |
|
* registered unarrived parties. If parent is non-null, this phaser |
383 |
|
* is registered with the parent and its initial phase number is |
384 |
|
* the same as that of parent phaser. |
416 |
|
/** |
417 |
|
* Adds the given number of new unarrived parties to this phaser. |
418 |
|
* |
419 |
< |
* @param parties the number of parties required to trip barrier |
419 |
> |
* @param parties the number of additional parties required to trip barrier |
420 |
|
* @return the arrival phase number to which this registration applied |
421 |
|
* @throws IllegalStateException if attempting to register more |
422 |
|
* than the maximum supported number of parties |
423 |
+ |
* @throws IllegalArgumentException if {@code parties < 0} |
424 |
|
*/ |
425 |
|
public int bulkRegister(int parties) { |
426 |
|
if (parties < 0) |
562 |
|
* Arrives at the barrier and awaits others. Equivalent in effect |
563 |
|
* to {@code awaitAdvance(arrive())}. If you need to await with |
564 |
|
* interruption or timeout, you can arrange this with an analogous |
565 |
< |
* construction using one of the other forms of the awaitAdvance |
566 |
< |
* method. If instead you need to deregister upon arrival use |
567 |
< |
* {@code arriveAndDeregister}. It is an unenforced usage error |
568 |
< |
* for an unregistered party to invoke this method. |
565 |
> |
* construction using one of the other forms of the {@code |
566 |
> |
* awaitAdvance} method. If instead you need to deregister upon |
567 |
> |
* arrival, use {@link #arriveAndDeregister}. It is an unenforced |
568 |
> |
* usage error for an unregistered party to invoke this method. |
569 |
|
* |
570 |
|
* @return the arrival phase number, or a negative number if terminated |
571 |
|
* @throws IllegalStateException if not terminated and the number |
828 |
|
volatile boolean wasInterrupted = false; |
829 |
|
volatile Thread thread; // nulled to cancel wait |
830 |
|
QNode next; |
831 |
+ |
|
832 |
|
QNode(Phaser phaser, int phase, boolean interruptible, |
833 |
|
boolean timed, long startTime, long nanos) { |
834 |
|
this.phaser = phaser; |
839 |
|
this.nanos = nanos; |
840 |
|
thread = Thread.currentThread(); |
841 |
|
} |
842 |
+ |
|
843 |
|
public boolean isReleasable() { |
844 |
|
return (thread == null || |
845 |
|
phaser.getPhase() != phase || |
846 |
|
(interruptible && wasInterrupted) || |
847 |
|
(timed && (nanos - (System.nanoTime() - startTime)) <= 0)); |
848 |
|
} |
849 |
+ |
|
850 |
|
public boolean block() { |
851 |
|
if (Thread.interrupted()) { |
852 |
|
wasInterrupted = true; |
863 |
|
} |
864 |
|
return isReleasable(); |
865 |
|
} |
866 |
+ |
|
867 |
|
void signal() { |
868 |
|
Thread t = thread; |
869 |
|
if (t != null) { |
871 |
|
LockSupport.unpark(t); |
872 |
|
} |
873 |
|
} |
874 |
+ |
|
875 |
|
boolean doWait() { |
876 |
|
if (thread != null) { |
877 |
|
try { |
878 |
|
ForkJoinPool.managedBlock(this); |
879 |
|
} catch (InterruptedException ie) { |
880 |
+ |
wasInterrupted = true; // can't currently happen |
881 |
|
} |
882 |
|
} |
883 |
|
return wasInterrupted; |
884 |
|
} |
880 |
– |
|
885 |
|
} |
886 |
|
|
887 |
|
/** |
923 |
|
node = new QNode(this, phase, false, false, 0, 0); |
924 |
|
else if (!queued) |
925 |
|
queued = tryEnqueue(node); |
926 |
< |
else |
927 |
< |
interrupted = node.doWait(); |
926 |
> |
else if (node.doWait()) |
927 |
> |
interrupted = true; |
928 |
|
} |
929 |
|
if (node != null) |
930 |
|
node.thread = null; |
950 |
|
node = new QNode(this, phase, true, false, 0, 0); |
951 |
|
else if (!queued) |
952 |
|
queued = tryEnqueue(node); |
953 |
< |
else |
954 |
< |
interrupted = node.doWait(); |
953 |
> |
else if (node.doWait()) |
954 |
> |
interrupted = true; |
955 |
|
} |
956 |
|
if (node != null) |
957 |
|
node.thread = null; |
982 |
|
node = new QNode(this, phase, true, true, startTime, nanos); |
983 |
|
else if (!queued) |
984 |
|
queued = tryEnqueue(node); |
985 |
< |
else |
986 |
< |
interrupted = node.doWait(); |
985 |
> |
else if (node.doWait()) |
986 |
> |
interrupted = true; |
987 |
|
} |
988 |
|
if (node != null) |
989 |
|
node.thread = null; |