From 840f7bad3c1a9ee41e258ed27d23dda719cce1eb Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Mon, 23 Jan 2017 08:19:11 -0800 Subject: [PATCH] Change delay time to double --- onjava/Nap.java | 21 ++++----------------- onjava/TimedAbort.java | 11 ++++++----- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/onjava/Nap.java b/onjava/Nap.java index bb76d32b..ba9df34a 100644 --- a/onjava/Nap.java +++ b/onjava/Nap.java @@ -6,28 +6,15 @@ package onjava; import java.util.concurrent.*; public class Nap { - // Seconds: - public Nap(int n) { + public Nap(double t) { // Seconds try { - TimeUnit.SECONDS.sleep(n); + TimeUnit.MILLISECONDS.sleep((int)(1000 * t)); } catch(InterruptedException e) { throw new RuntimeException(e); } } - // Fractions of a second: - public Nap(double d) { - try { - TimeUnit.MILLISECONDS.sleep((int)(1000 * d)); - } catch(InterruptedException e) { - throw new RuntimeException(e); - } - } - public Nap(int n, String msg) { - this(n); - System.out.println(msg); - } - public Nap(double d, String msg) { - this(d); + public Nap(double t, String msg) { + this(t); System.out.println(msg); } } diff --git a/onjava/TimedAbort.java b/onjava/TimedAbort.java index bee2adcc..b378738d 100644 --- a/onjava/TimedAbort.java +++ b/onjava/TimedAbort.java @@ -2,18 +2,19 @@ // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. -// Terminate a program after n seconds +// Terminate a program after t seconds package onjava; import java.util.concurrent.*; public class TimedAbort { private volatile boolean restart = true; - public TimedAbort(int n, String msg) { + public TimedAbort(double t, String msg) { CompletableFuture.runAsync(() -> { try { while(restart) { restart = false; - TimeUnit.SECONDS.sleep(n); + TimeUnit.MILLISECONDS + .sleep((int)(1000 * t)); } } catch(InterruptedException e) { throw new RuntimeException(e); @@ -22,8 +23,8 @@ public class TimedAbort { System.exit(0); }); } - public TimedAbort(int n) { - this(n, "TimedAbort " + n); + public TimedAbort(double t) { + this(t, "TimedAbort " + t); } public void restart() { restart = true; } }