Skip to content

Commit 13a4be1

Browse files
committed
clean: Use Threads.sleep() utility in SlowTask
1 parent 5f34c1b commit 13a4be1

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/dev/enola/be/task/Task.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static java.util.Objects.requireNonNull;
44

5+
import dev.enola.common.concurrent.Threads;
6+
57
import java.time.Duration;
68
import java.time.Instant;
79
import java.util.Optional;
@@ -30,10 +32,15 @@ protected Task(I input) {
3032
* <p>This method is called by the {@link TaskExecutor} when the task is executed. It will run
3133
* in a virtual thread of modern Java. For long-running tasks, please ensure to periodically
3234
* check for interruption via {@link Thread#isInterrupted()} and terminate early if so, by
33-
* throwing {@link InterruptedException}. Please do not, under any circumstances, use the
34-
* ancient {@link Thread#yield()} method within implementations of this method (or anywhere
35-
* really anymore, nowadays); as it will cause performance degradation by a factor of x100 for
36-
* no benefit at all anymore on modern Java, especially on virtual threads.
35+
* throwing {@link InterruptedException}.
36+
*
37+
* <p>Please do not, under any circumstances, use the ancient {@link Thread#yield()} method
38+
* within implementations of this method (or anywhere really anymore, nowadays); as it will
39+
* cause performance degradation by a factor of x100 for no benefit at all anymore on modern
40+
* Java, especially on virtual threads. If you must "simulate work", then please use our {@link
41+
* Threads#sleep(Duration)} utility (instead of the JDK {@link Thread#sleep(long)}), which
42+
* correctly handles interruption. (But this should really should only be required in tests and
43+
* demos, not ever for any real work load.)
3744
*
3845
* @return output, never null (use {@link Empty#INSTANCE}; or {@link Optional}, if needed)
3946
* @throws Exception in case of any failure

src/dev/enola/be/task/demo/LongIncrementingTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.enola.be.task.TaskExecutor;
77
import dev.enola.be.task.demo.LongIncrementingTask.Input;
88
import dev.enola.be.task.demo.LongIncrementingTask.Output;
9+
import dev.enola.common.concurrent.Threads;
910
import dev.enola.common.function.CheckedConsumer;
1011
import dev.enola.common.log.JulConfigurer;
1112

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package dev.enola.be.task.test;
22

33
import dev.enola.be.task.Task;
4+
import dev.enola.common.concurrent.Threads;
45

56
import java.time.Duration;
67

78
public class SlowTask extends Task<String, String> {
89

9-
private final long sleepMillis;
10+
private final Duration sleep;
1011
private final Duration timeout;
1112

1213
public SlowTask(String input, long sleepMillis, Duration timeout) {
1314
super(input);
14-
this.sleepMillis = sleepMillis;
15+
this.sleep = Duration.ofMillis(sleepMillis);
1516
this.timeout = timeout;
1617
}
1718

@@ -26,13 +27,7 @@ public Duration timeout() {
2627

2728
@Override
2829
protected String execute() throws Exception {
29-
try {
30-
Thread.sleep(sleepMillis);
31-
return "Completed: " + input;
32-
33-
} catch (InterruptedException e) {
34-
Thread.currentThread().interrupt();
35-
throw e;
36-
}
30+
Threads.sleep(sleep);
31+
return "Completed: " + input;
3732
}
3833
}

src/dev/enola/be/task/demo/Threads.java renamed to src/dev/enola/common/concurrent/Threads.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package dev.enola.be.task.demo;
1+
package dev.enola.common.concurrent;
22

33
import java.time.Duration;
44

5-
final class Threads {
5+
public final class Threads {
66

77
// from
88
// https://github.com/enola-dev/enola/blob/main/java/dev/enola/common/concurrent/Threads.java
@@ -19,8 +19,8 @@ final class Threads {
1919
* article</a>, or <a
2020
* href="https://www.yegor256.com/2015/10/20/interrupted-exception.html">yegor256.com Blog
2121
* Post</a> and <a href="https://github.com/google/guava/issues/1219">Google Guava Issue
22-
* #1219</a>, as well as Google Guava's Uninterruptibles.sleepUninterruptibly(Duration) (which does
23-
* something different from this).
22+
* #1219</a>, as well as Google Guava's Uninterruptibles.sleepUninterruptibly(Duration) (which
23+
* does something different from this).
2424
*
2525
* @param duration Duration to sleep
2626
* @throws InterruptedException if interrupted

0 commit comments

Comments
 (0)