From 4ed86f74205baf17c5647f64e8d3188dfd2dc14e Mon Sep 17 00:00:00 2001 From: fabianArbeit Date: Tue, 14 Jan 2025 11:39:58 +0100 Subject: [PATCH] =?UTF-8?q?-=20ProgressHandler=20Threadsafe=20gemacht=20-?= =?UTF-8?q?=20erm=C3=B6gliche=20loggen=20ohne=20progress,=20rein=20Zeitbas?= =?UTF-8?q?iert=20=20=20->=20m=C3=B6glichst,=20sodass=20kein=20spam=20ents?= =?UTF-8?q?teht=20bzw.=20das=20regul=C3=A4re=20Loggen=20ber=C3=BCcksichtig?= =?UTF-8?q?t=20wird?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FabianUtil.java | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) 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(); + } }