forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#6977 from initialcommit-io/BAEL-2937
BAEL-2937
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
core-java-modules/core-java-8-2/src/main/java/com/baeldung/delay/Delay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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) { | ||
|
||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
executorService.schedule(Delay::someTask1, delayInSeconds, TimeUnit.SECONDS); | ||
|
||
} | ||
|
||
private static void fixedRateServiceTask(Integer delayInSeconds) { | ||
|
||
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."); | ||
} | ||
|
||
} |