Wozu eigentlich Atmoic?

master
Selebrator 7 years ago
parent 81be577d01
commit de32d5fd23

@ -1,13 +1,10 @@
package _11._2.alternative; package _11._2.alternative;
import java.util.concurrent.atomic.AtomicInteger;
public class Checkpoint { public class Checkpoint {
private static AtomicInteger GATHERED = new AtomicInteger(0);
private final Object lock = new Object(); private final Object lock = new Object();
private int groupSize; private int groupSize;
private int gathered = 0;
public Checkpoint(int groupSize) { public Checkpoint(int groupSize) {
this.groupSize = groupSize; this.groupSize = groupSize;
@ -19,10 +16,10 @@ public class Checkpoint {
*/ */
public void gather() throws InterruptedException { public void gather() throws InterruptedException {
synchronized(this.lock) { synchronized(this.lock) {
if(GATHERED.incrementAndGet() != this.groupSize) { if(++this.gathered != this.groupSize) {
this.lock.wait(); this.lock.wait();
} else { } else {
GATHERED.set(0); this.gathered = 0;
System.out.println("Group gathered"); System.out.println("Group gathered");
this.lock.notifyAll(); this.lock.notifyAll();
} }

Loading…
Cancel
Save