From 99f44b344486f1f06efdf87cdc2204dc93a2f21a Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Thu, 16 May 2019 22:38:21 -0700 Subject: [PATCH 1/3] Add package demonstrating how to delay code execution in java --- .../main/java/com/baeldung/delay/Delay.java | 95 +++++++++++++++++++ .../com/baeldung/delay/DelayUnitTest.java | 14 +++ 2 files changed, 109 insertions(+) create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java create mode 100644 core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java new file mode 100644 index 0000000000..34d4e0ce85 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java @@ -0,0 +1,95 @@ +package com.baeldung.delay; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class Delay { + + public static void main(String args[]) throws InterruptedException { + + threadSleep(4, 1); + + timeunitSleep(4, 1); + + delayedServiceTask(5); + + fixedRateServiceTask(5); + + System.out.println("Done."); + + return; + + } + + private static void threadSleep(Integer iterations, Integer secondsToSleep) { + + for (Integer i = 0; i < iterations; i++) { + + System.out.println("This is loop iteration number " + i.toString()); + + try { + Thread.sleep(secondsToSleep * 1000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + } + + } + + private static void timeunitSleep(Integer iterations, Integer secondsToSleep) { + + for (Integer i = 0; i < iterations; i++) { + + System.out.println("This is loop iteration number " + i.toString()); + + try { + TimeUnit.SECONDS.sleep(secondsToSleep); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + } + + } + + private static void delayedServiceTask(Integer delayInSeconds) { + + final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + + executorService.schedule(Delay::someTask1, delayInSeconds, TimeUnit.SECONDS); + + } + + private static void fixedRateServiceTask(Integer delayInSeconds) { + + final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + + ScheduledFuture sf = executorService.scheduleAtFixedRate(Delay::someTask2, 0, delayInSeconds, + TimeUnit.SECONDS); + + try { + TimeUnit.SECONDS.sleep(20); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + sf.cancel(true); + + } + + private static void someTask1() { + System.out.println("Task 1 completed."); + } + + private static void someTask2() { + System.out.println("Task 2 completed."); + } + + public static int main(Boolean isTest) { + return 0; + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java b/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java new file mode 100644 index 0000000000..5be2fb4b69 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java @@ -0,0 +1,14 @@ +package com.baeldung.delay; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class DelayUnitTest { + + @Test + void givenMain_returnZero() { + assertEquals(Delay.main(true), 0); + } + +} \ No newline at end of file From 768b82eec39290df145258f30e77707a587d0d62 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Sat, 18 May 2019 20:39:07 -0700 Subject: [PATCH 2/3] Remove unit test --- .../src/main/java/com/baeldung/delay/Delay.java | 4 ---- .../java/com/baeldung/delay/DelayUnitTest.java | 14 -------------- 2 files changed, 18 deletions(-) delete mode 100644 core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java index 34d4e0ce85..36b7ed7ffb 100644 --- a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java @@ -87,9 +87,5 @@ public class Delay { private static void someTask2() { System.out.println("Task 2 completed."); } - - public static int main(Boolean isTest) { - return 0; - } } \ No newline at end of file diff --git a/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java b/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java deleted file mode 100644 index 5be2fb4b69..0000000000 --- a/core-java-modules/core-java-8-2/src/test/java/com/baeldung/delay/DelayUnitTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.delay; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class DelayUnitTest { - - @Test - void givenMain_returnZero() { - assertEquals(Delay.main(true), 0); - } - -} \ No newline at end of file From d67dd3cb5b8417b3679c0f4e0caa81886fa085f8 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Sat, 18 May 2019 22:06:36 -0700 Subject: [PATCH 3/3] Remove 'final' from ExecutorService declarations --- .../core-java-8-2/src/main/java/com/baeldung/delay/Delay.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java index 36b7ed7ffb..0cc0c9487f 100644 --- a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java @@ -57,7 +57,7 @@ public class Delay { private static void delayedServiceTask(Integer delayInSeconds) { - final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.schedule(Delay::someTask1, delayInSeconds, TimeUnit.SECONDS); @@ -65,7 +65,7 @@ public class Delay { private static void fixedRateServiceTask(Integer delayInSeconds) { - final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture sf = executorService.scheduleAtFixedRate(Delay::someTask2, 0, delayInSeconds, TimeUnit.SECONDS);