Fix thread-safety
properly do time based logging and quit, when done
This commit is contained in:
parent
6e1fb749ad
commit
71daaaff15
@ -10,6 +10,7 @@ import java.text.DecimalFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FabianUtil {
|
||||
|
||||
@ -72,7 +73,12 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public void tickAndLog() {
|
||||
protected synchronized int getTimeLogDistance() {
|
||||
return timeLogDistance;
|
||||
}
|
||||
|
||||
|
||||
public synchronized void tickAndLog() {
|
||||
tick();
|
||||
logIf();
|
||||
}
|
||||
@ -83,26 +89,36 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public void logAndTick() {
|
||||
public synchronized void logAndTick() {
|
||||
logIf();
|
||||
tick();
|
||||
}
|
||||
|
||||
|
||||
public synchronized void logIf() {
|
||||
if (counter.remainder(logDistance).signum() == 0 || isBored()) {
|
||||
if (shouldLog()) {
|
||||
log();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private synchronized boolean isBored() {
|
||||
ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt;
|
||||
return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS) >= timeLogDistance;
|
||||
protected synchronized boolean shouldLog() {
|
||||
return counter.remainder(logDistance).signum() == 0 || isBored();
|
||||
}
|
||||
|
||||
|
||||
public void log() {
|
||||
private synchronized boolean isBored() {
|
||||
return getTimeSinceLastLogged() >= timeLogDistance;
|
||||
}
|
||||
|
||||
|
||||
protected synchronized long getTimeSinceLastLogged() {
|
||||
ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt;
|
||||
return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
public synchronized void log() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
updateLastLoggedAt();
|
||||
@ -160,7 +176,13 @@ public class FabianUtil {
|
||||
|
||||
|
||||
@Override
|
||||
public void log() {
|
||||
protected synchronized boolean shouldLog() {
|
||||
return super.shouldLog() && getCounter().compareTo(total) <= 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void log() {
|
||||
BigDecimal counter = getCounter();
|
||||
BigDecimal percentage = BigDecimal.valueOf(100).divide(total, MathContext.DECIMAL64).multiply(counter);
|
||||
Duration timePassed = getTimePassed();
|
||||
@ -182,7 +204,21 @@ public class FabianUtil {
|
||||
|
||||
|
||||
public void logContinuouslyTimeBased() {
|
||||
new Thread(this::logIf).start();
|
||||
new Thread(this::logTimeBased).start();
|
||||
}
|
||||
|
||||
|
||||
private synchronized void logTimeBased() {
|
||||
while (getCounter().compareTo(total) != 0) {
|
||||
logIf();
|
||||
long timeSinceLastLogged = getTimeSinceLastLogged();
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(timeSinceLastLogged - getTimeLogDistance());
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user