-
Notifications
You must be signed in to change notification settings - Fork 1
clean: Thread.sleep() util; demo simpleLoop() w.o. output #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||
| import dev.enola.be.task.demo.LongIncrementingTask.Output; | ||||||
|
|
||||||
| import java.time.Duration; | ||||||
| import java.time.Instant; | ||||||
| import java.util.function.Consumer; | ||||||
|
|
||||||
| public class LongIncrementingTask extends Task<Input, Output> { | ||||||
|
|
@@ -28,26 +29,39 @@ protected Output execute() throws Exception { | |||||
| Thread.yield(); | ||||||
| if (Thread.currentThread().isInterrupted()) | ||||||
| throw new InterruptedException("Task was interrupted"); | ||||||
| try { | ||||||
| Thread.sleep(input.sleep.toMillis()); | ||||||
| } catch (InterruptedException e) { | ||||||
| Thread.currentThread().interrupt(); | ||||||
| throw new InterruptedException("Task was interrupted during sleep"); | ||||||
| } | ||||||
| Threads.sleep(input.sleep); | ||||||
| } | ||||||
|
|
||||||
| // TODO Report % progress | ||||||
|
|
||||||
| return new Output(input.max); | ||||||
| } | ||||||
|
|
||||||
| public static void main(String[] args) { | ||||||
| // Count to 10000, with 1ms pause between each increment | ||||||
| var input = new Input(10000, Duration.ofMillis(1)); | ||||||
| private static void simpleLoop(long max, Duration sleep) throws InterruptedException { | ||||||
| var start = Instant.now(); | ||||||
| for (long i = 0; i < max; i++) Threads.sleep(sleep); | ||||||
| var duration = Duration.between(start, Instant.now()); | ||||||
| System.out.println( | ||||||
| "Looped to " | ||||||
| + max | ||||||
| + " with " | ||||||
| + sleep | ||||||
| + " sleep, but without output, in " | ||||||
| + duration); | ||||||
|
Comment on lines
+44
to
+50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| } | ||||||
|
|
||||||
| public static void main(String[] args) throws InterruptedException { | ||||||
| // Count to max, with 1ms pause between each increment | ||||||
| var max = 10000; | ||||||
| var sleep = Duration.ofMillis(0); | ||||||
|
||||||
| var sleep = Duration.ofMillis(0); | |
| var sleep = Duration.ofMillis(1); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package dev.enola.be.task.demo; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| final class Threads { | ||
|
|
||
| // from | ||
| // https://github.com/enola-dev/enola/blob/main/java/dev/enola/common/concurrent/Threads.java | ||
|
|
||
| /** | ||
| * Sleep 😴 for a certain duration. | ||
| * | ||
| * <p>This is a wrapper around {@link Thread#sleep(Duration)} which (correctly) handles its | ||
| * {@link InterruptedException} by (re-)interrupting the current thread, and then re-throwing it | ||
| * as a checked exception. It also checks for negative duration (which the original method just | ||
| * ignores, which could hide bugs), and has an optimizing shortcut for duration 0. | ||
|
Comment on lines
+15
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Javadoc states that this wrapper checks for negative duration which the "original method" ignores. This is true for |
||
| * | ||
| * <p>See <a href="https://www.baeldung.com/java-interrupted-exception">Baeldung's related | ||
| * article</a>, or <a | ||
| * href="https://www.yegor256.com/2015/10/20/interrupted-exception.html">yegor256.com Blog | ||
| * Post</a> and <a href="https://github.com/google/guava/issues/1219">Google Guava Issue | ||
| * #1219</a>, as well as Google Guava's Uninterruptibles.sleepUninterruptibly(Duration) (which does | ||
| * something different from this). | ||
| * | ||
| * @param duration Duration to sleep | ||
| * @throws InterruptedException if interrupted | ||
| * @throws IllegalArgumentException if duration is negative | ||
| */ | ||
| public static void sleep(Duration duration) throws InterruptedException { | ||
| if (duration.isNegative()) | ||
| throw new IllegalArgumentException(duration + " cannot be negative"); | ||
|
|
||
| if (duration.isZero()) return; | ||
|
|
||
| try { | ||
| Thread.sleep(duration); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| // WAS: throw new UncheckedInterruptedException(e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private Threads() {} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop calls Threads.sleep() for each iteration without any yield or interrupt check. For large values of max, this could create performance issues and make the method unresponsive to interruption. Consider adding periodic interrupt checks similar to the main execute() method.