diff --git a/pom.xml b/pom.xml index e032178..dcd8b58 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,10 @@ heroesleo65 Ilya Korobtsev + + kmtong + KM Tong + @@ -122,28 +126,28 @@ org.junit.jupiter junit-jupiter-api - 5.7.2 + 5.8.1 test org.junit.jupiter junit-jupiter-engine - 5.7.2 + 5.8.1 test org.slf4j slf4j-api - 1.7.31 + 1.7.32 test org.slf4j slf4j-simple - 1.7.31 + 1.7.32 test diff --git a/src/main/java/me/tongfei/progressbar/ProgressBar.java b/src/main/java/me/tongfei/progressbar/ProgressBar.java index 9e4f6ff..ecd23bd 100644 --- a/src/main/java/me/tongfei/progressbar/ProgressBar.java +++ b/src/main/java/me/tongfei/progressbar/ProgressBar.java @@ -163,7 +163,6 @@ public void close() { scheduledTask.cancel(false); progress.kill(); try { - action.setForceRefresh(); Util.executor.schedule(action, 0, TimeUnit.NANOSECONDS).get(); } catch (InterruptedException | ExecutionException e) { /* NOOP */ } } diff --git a/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java b/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java index b70dc83..86c8502 100644 --- a/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java +++ b/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java @@ -8,8 +8,8 @@ class ProgressUpdateAction implements Runnable { ProgressState progress; private ProgressBarRenderer renderer; private ProgressBarConsumer consumer; - private long last; - private boolean forceRefresh = false; + volatile private long last; + volatile private boolean first; ProgressUpdateAction( ProgressState progress, @@ -20,26 +20,32 @@ class ProgressUpdateAction implements Runnable { this.renderer = renderer; this.consumer = consumer; this.last = progress.start; + this.first = true; } private void refresh() { - if (forceRefresh || progress.current > last) { - String rendered = renderer.render(progress, consumer.getMaxRenderedLength()); - consumer.accept(rendered); - last = progress.current; - } + if (progress.current > last) forceRefresh(); // else do nothing: only print when actual progress is made (#91). } - public void setForceRefresh() { - forceRefresh = true; + public void forceRefresh() { + String rendered = renderer.render(progress, consumer.getMaxRenderedLength()); + consumer.accept(rendered); + last = progress.current; } public void run() { - if (!progress.paused) refresh(); - if (!progress.alive) { - consumer.close(); - TerminalUtils.closeTerminal(); + if (first) { + forceRefresh(); + first = false; + } + else { + if (!progress.paused) refresh(); + if (!progress.alive) { + forceRefresh(); + consumer.close(); + TerminalUtils.closeTerminal(); + } } } diff --git a/src/test/java/me/tongfei/progressbar/Issue117Test.java b/src/test/java/me/tongfei/progressbar/Issue117Test.java new file mode 100644 index 0000000..011725c --- /dev/null +++ b/src/test/java/me/tongfei/progressbar/Issue117Test.java @@ -0,0 +1,17 @@ +package me.tongfei.progressbar; + +import org.junit.jupiter.api.Test; + +public class Issue117Test { + + @Test + void testImmediateStart() throws InterruptedException { + try (ProgressBar pb = new ProgressBar("Test", 2)) { + for (int i = 0; i < 2; i++) { + Thread.sleep(1000); + pb.step(); + } + } + } + +}