diff --git a/FabianUtil.java b/FabianUtil.java index d6d827e..c299ab8 100644 --- a/FabianUtil.java +++ b/FabianUtil.java @@ -1,3 +1,5 @@ +package de.vorsorge.theo.util; + import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; @@ -65,7 +67,7 @@ public class FabianUtil { } - protected BigDecimal getCounter() { + protected synchronized BigDecimal getCounter() { return counter; } @@ -76,7 +78,7 @@ public class FabianUtil { } - protected void tick() { + protected synchronized void tick() { counter = counter.add(BigDecimal.ONE); } @@ -87,14 +89,14 @@ public class FabianUtil { } - public void logIf() { + public synchronized void logIf() { if (counter.remainder(logDistance).signum() == 0 || isBored()) { log(); } } - private boolean isBored() { + private synchronized boolean isBored() { ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt; return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS) >= timeLogDistance; } @@ -107,12 +109,12 @@ public class FabianUtil { } - protected void updateLastLoggedAt() { + protected synchronized void updateLastLoggedAt() { lastLoggedAt = ZonedDateTime.now(); } - protected Duration getTimePassed() { + protected synchronized Duration getTimePassed() { return Duration.ofSeconds(startTime.until(ZonedDateTime.now(), ChronoUnit.SECONDS)); } @@ -127,7 +129,7 @@ public class FabianUtil { } - public void logFinal() { + public synchronized void logFinal() { Duration timePassed = getTimePassed(); System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen."); } @@ -178,6 +180,11 @@ public class FabianUtil { + formatDuration(timePassed) + " vergangen" + remainingTimePart + "."); updateLastLoggedAt(); } + + + public void logContinuouslyTimeBased() { + new Thread(this::logIf).start(); + } }