File tree Expand file tree Collapse file tree 2 files changed +10
-22
lines changed
Expand file tree Collapse file tree 2 files changed +10
-22
lines changed Original file line number Diff line number Diff line change 66import java .util .concurrent .*;
77
88public 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}
Original file line number Diff line number Diff line change 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
66package onjava ;
77import java .util .concurrent .*;
88
99public 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}
You can’t perform that action at this time.
0 commit comments