Skip to content

Commit 840f7ba

Browse files
committed
Change delay time to double
1 parent 04039c7 commit 840f7ba

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

onjava/Nap.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,15 @@
66
import java.util.concurrent.*;
77

88
public class Nap {
9-
// Seconds:
10-
public Nap(int n) {
9+
public Nap(double t) { // Seconds
1110
try {
12-
TimeUnit.SECONDS.sleep(n);
11+
TimeUnit.MILLISECONDS.sleep((int)(1000 * t));
1312
} catch(InterruptedException e) {
1413
throw new RuntimeException(e);
1514
}
1615
}
17-
// Fractions of a second:
18-
public Nap(double d) {
19-
try {
20-
TimeUnit.MILLISECONDS.sleep((int)(1000 * d));
21-
} catch(InterruptedException e) {
22-
throw new RuntimeException(e);
23-
}
24-
}
25-
public Nap(int n, String msg) {
26-
this(n);
27-
System.out.println(msg);
28-
}
29-
public Nap(double d, String msg) {
30-
this(d);
16+
public Nap(double t, String msg) {
17+
this(t);
3118
System.out.println(msg);
3219
}
3320
}

onjava/TimedAbort.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
// (c)2017 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Terminate a program after n seconds
5+
// Terminate a program after t seconds
66
package onjava;
77
import java.util.concurrent.*;
88

99
public class TimedAbort {
1010
private volatile boolean restart = true;
11-
public TimedAbort(int n, String msg) {
11+
public TimedAbort(double t, String msg) {
1212
CompletableFuture.runAsync(() -> {
1313
try {
1414
while(restart) {
1515
restart = false;
16-
TimeUnit.SECONDS.sleep(n);
16+
TimeUnit.MILLISECONDS
17+
.sleep((int)(1000 * t));
1718
}
1819
} catch(InterruptedException e) {
1920
throw new RuntimeException(e);
@@ -22,8 +23,8 @@ public TimedAbort(int n, String msg) {
2223
System.exit(0);
2324
});
2425
}
25-
public TimedAbort(int n) {
26-
this(n, "TimedAbort " + n);
26+
public TimedAbort(double t) {
27+
this(t, "TimedAbort " + t);
2728
}
2829
public void restart() { restart = true; }
2930
}

0 commit comments

Comments
 (0)