Merge pull request #6977 from initialcommit-io/BAEL-2937

BAEL-2937
This commit is contained in:
Eric Martin 2019-05-20 23:10:52 -05:00 committed by GitHub
commit dd160bf2fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 91 additions and 0 deletions

View File

@ -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.");
}
}