93 |
|
* idiom is for the method setting this up to first register, then |
94 |
|
* start the actions, then deregister, as in: |
95 |
|
* |
96 |
< |
* <pre> |
97 |
< |
* void runTasks(List<Runnable> list) { |
98 |
< |
* final Phaser phaser = new Phaser(1); // "1" to register self |
99 |
< |
* for (Runnable r : list) { |
100 |
< |
* phaser.register(); |
101 |
< |
* new Thread() { |
102 |
< |
* public void run() { |
103 |
< |
* phaser.arriveAndAwaitAdvance(); // await all creation |
104 |
< |
* r.run(); |
105 |
< |
* phaser.arriveAndDeregister(); // signal completion |
106 |
< |
* } |
107 |
< |
* }.start(); |
96 |
> |
* <pre> {@code |
97 |
> |
* void runTasks(List<Runnable> list) { |
98 |
> |
* final Phaser phaser = new Phaser(1); // "1" to register self |
99 |
> |
* for (Runnable r : list) { |
100 |
> |
* phaser.register(); |
101 |
> |
* new Thread() { |
102 |
> |
* public void run() { |
103 |
> |
* phaser.arriveAndAwaitAdvance(); // await all creation |
104 |
> |
* r.run(); |
105 |
> |
* phaser.arriveAndDeregister(); // signal completion |
106 |
> |
* } |
107 |
> |
* }.start(); |
108 |
|
* } |
109 |
|
* |
110 |
|
* doSomethingOnBehalfOfWorkers(); |
113 |
|
* p = phaser.awaitAdvance(p); // ... and await arrival |
114 |
|
* otherActions(); // do other things while tasks execute |
115 |
|
* phaser.awaitAdvance(p); // await final completion |
116 |
< |
* } |
117 |
< |
* </pre> |
116 |
> |
* }}</pre> |
117 |
|
* |
118 |
|
* <p>One way to cause a set of threads to repeatedly perform actions |
119 |
|
* for a given number of iterations is to override {@code onAdvance}: |
120 |
|
* |
121 |
< |
* <pre> |
122 |
< |
* void startTasks(List<Runnable> list, final int iterations) { |
123 |
< |
* final Phaser phaser = new Phaser() { |
124 |
< |
* public boolean onAdvance(int phase, int registeredParties) { |
125 |
< |
* return phase >= iterations || registeredParties == 0; |
121 |
> |
* <pre> {@code |
122 |
> |
* void startTasks(List<Runnable> list, final int iterations) { |
123 |
> |
* final Phaser phaser = new Phaser() { |
124 |
> |
* public boolean onAdvance(int phase, int registeredParties) { |
125 |
> |
* return phase >= iterations || registeredParties == 0; |
126 |
> |
* } |
127 |
> |
* }; |
128 |
> |
* phaser.register(); |
129 |
> |
* for (Runnable r : list) { |
130 |
> |
* phaser.register(); |
131 |
> |
* new Thread() { |
132 |
> |
* public void run() { |
133 |
> |
* do { |
134 |
> |
* r.run(); |
135 |
> |
* phaser.arriveAndAwaitAdvance(); |
136 |
> |
* } while(!phaser.isTerminated(); |
137 |
|
* } |
138 |
< |
* }; |
129 |
< |
* phaser.register(); |
130 |
< |
* for (Runnable r : list) { |
131 |
< |
* phaser.register(); |
132 |
< |
* new Thread() { |
133 |
< |
* public void run() { |
134 |
< |
* do { |
135 |
< |
* r.run(); |
136 |
< |
* phaser.arriveAndAwaitAdvance(); |
137 |
< |
* } while(!phaser.isTerminated(); |
138 |
< |
* } |
139 |
< |
* }.start(); |
138 |
> |
* }.start(); |
139 |
|
* } |
140 |
|
* phaser.arriveAndDeregister(); // deregister self, don't wait |
141 |
< |
* } |
143 |
< |
* </pre> |
141 |
> |
* }}</pre> |
142 |
|
* |
143 |
|
* <p> To create a set of tasks using a tree of Phasers, |
144 |
|
* you could use code of the following form, assuming a |
145 |
|
* Task class with a constructor accepting a Phaser that |
146 |
|
* it registers for upon construction: |
147 |
< |
* <pre> |
148 |
< |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
149 |
< |
* int step = (hi - lo) / TASKS_PER_PHASER; |
150 |
< |
* if (step > 1) { |
151 |
< |
* int i = lo; |
152 |
< |
* while (i < hi) { |
153 |
< |
* int r = Math.min(i + step, hi); |
154 |
< |
* build(actions, i, r, new Phaser(b)); |
155 |
< |
* i = r; |
156 |
< |
* } |
157 |
< |
* } |
158 |
< |
* else { |
159 |
< |
* for (int i = lo; i < hi; ++i) |
160 |
< |
* actions[i] = new Task(b); |
161 |
< |
* // assumes new Task(b) performs b.register() |
162 |
< |
* } |
163 |
< |
* } |
164 |
< |
* // .. initially called, for n tasks via |
167 |
< |
* build(new Task[n], 0, n, new Phaser()); |
168 |
< |
* </pre> |
147 |
> |
* <pre> {@code |
148 |
> |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
149 |
> |
* int step = (hi - lo) / TASKS_PER_PHASER; |
150 |
> |
* if (step > 1) { |
151 |
> |
* int i = lo; |
152 |
> |
* while (i < hi) { |
153 |
> |
* int r = Math.min(i + step, hi); |
154 |
> |
* build(actions, i, r, new Phaser(b)); |
155 |
> |
* i = r; |
156 |
> |
* } |
157 |
> |
* } else { |
158 |
> |
* for (int i = lo; i < hi; ++i) |
159 |
> |
* actions[i] = new Task(b); |
160 |
> |
* // assumes new Task(b) performs b.register() |
161 |
> |
* } |
162 |
> |
* } |
163 |
> |
* // .. initially called, for n tasks via |
164 |
> |
* build(new Task[n], 0, n, new Phaser());}</pre> |
165 |
|
* |
166 |
|
* The best value of {@code TASKS_PER_PHASER} depends mainly on |
167 |
|
* expected barrier synchronization rates. A value as low as four may |
395 |
|
phase = phaseOf(s); |
396 |
|
int unarrived = unarrivedOf(s) + registrations; |
397 |
|
int parties = partiesOf(s) + registrations; |
398 |
< |
if (phase < 0) |
398 |
> |
if (phase < 0) |
399 |
|
break; |
400 |
|
if (parties > ushortMask || unarrived > ushortMask) |
401 |
|
throw new IllegalStateException(badBounds(parties, unarrived)); |
553 |
|
* @return the phase on exit from this method |
554 |
|
* @throws InterruptedException if thread interrupted while waiting |
555 |
|
*/ |
556 |
< |
public int awaitAdvanceInterruptibly(int phase) |
556 |
> |
public int awaitAdvanceInterruptibly(int phase) |
557 |
|
throws InterruptedException { |
558 |
|
if (phase < 0) |
559 |
|
return phase; |
791 |
|
try { |
792 |
|
ForkJoinPool.managedBlock(this, false); |
793 |
|
} catch (InterruptedException ie) { |
794 |
< |
} |
794 |
> |
} |
795 |
|
} |
796 |
|
return wasInterrupted; |
797 |
|
} |
929 |
|
throws NoSuchFieldException, IllegalAccessException { |
930 |
|
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
931 |
|
f.setAccessible(true); |
932 |
< |
return (Unsafe)f.get(null); |
932 |
> |
return (Unsafe) f.get(null); |
933 |
|
} |
934 |
|
|
935 |
|
private static long fieldOffset(String fieldName) |
945 |
|
try { |
946 |
|
_unsafe = getUnsafe(); |
947 |
|
stateOffset = fieldOffset("state"); |
948 |
< |
} catch (Exception e) { |
948 |
> |
} catch (Throwable e) { |
949 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
950 |
|
} |
951 |
|
} |