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