CountUpAndDownLatch remove references to CountDownLatch

This commit is contained in:
Bixilon 2020-11-02 15:46:44 +01:00
parent d2377c7586
commit 058fee0481
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -13,67 +13,55 @@
package de.bixilon.minosoft.util; package de.bixilon.minosoft.util;
import java.util.concurrent.CountDownLatch;
// Thanks https://stackoverflow.com/questions/14255019/latch-that-can-be-incremented // Thanks https://stackoverflow.com/questions/14255019/latch-that-can-be-incremented
public class CountUpAndDownLatch { public class CountUpAndDownLatch {
private final Object lock = new Object(); private final Object lock = new Object();
private CountDownLatch latch; private long count;
private long total = 0; private long total = 0;
public CountUpAndDownLatch(int count) { public CountUpAndDownLatch(int count) {
total += count; total = count;
this.latch = new CountDownLatch(count); this.count = count;
} }
public CountUpAndDownLatch() { public CountUpAndDownLatch() {
total = 1; total = 1;
this.latch = new CountDownLatch(1); this.count = 1;
}
public void countDownOrWaitIfZero() throws InterruptedException {
synchronized (lock) {
while (latch.getCount() == 0) {
lock.wait();
}
latch.countDown();
lock.notifyAll();
}
} }
public void waitUntilZero() throws InterruptedException { public void waitUntilZero() throws InterruptedException {
synchronized (lock) { synchronized (lock) {
while (latch.getCount() != 0) { while (count > 0) {
lock.wait(); lock.wait();
} }
} }
} }
public void countUp() { //should probably check for Integer.MAX_VALUE public void countUp() {
synchronized (lock) { synchronized (lock) {
total++; total++;
latch = new CountDownLatch((int) latch.getCount() + 1); count++;
lock.notifyAll(); lock.notifyAll();
} }
} }
public void countDown() { //should probably check for Integer.MAX_VALUE public void countDown() {
synchronized (lock) { synchronized (lock) {
latch.countDown(); count--;
lock.notifyAll(); lock.notifyAll();
} }
} }
public int getCount() { public long getCount() {
synchronized (lock) { synchronized (lock) {
return (int) latch.getCount(); return count;
} }
} }
public void setCount(int value) { public void setCount(int value) {
synchronized (lock) { synchronized (lock) {
total += value; total += value;
latch = new CountDownLatch(value); count = value;
lock.notifyAll(); lock.notifyAll();
} }
} }
@ -81,7 +69,7 @@ public class CountUpAndDownLatch {
public void addCount(int count) { public void addCount(int count) {
synchronized (lock) { synchronized (lock) {
total += count; total += count;
latch = new CountDownLatch((int) latch.getCount() + count); this.count += count;
lock.notifyAll(); lock.notifyAll();
} }
} }
@ -91,10 +79,10 @@ public class CountUpAndDownLatch {
} }
public void waitForChange() throws InterruptedException { public void waitForChange() throws InterruptedException {
long current = latch.getCount(); long latestCount = count;
long total = this.total; long latestTotal = this.total;
synchronized (lock) { synchronized (lock) {
while (current == latch.getCount() && total == this.total) { while (latestCount == count && latestTotal == total) {
lock.wait(); lock.wait();
} }
} }
@ -102,6 +90,6 @@ public class CountUpAndDownLatch {
@Override @Override
public String toString() { public String toString() {
return String.format("%d / %d", latch.getCount(), total); return String.format("%d / %d", count, total);
} }
} }