Skip to content

Commit

Permalink
Merge pull request eugenp#6977 from initialcommit-io/BAEL-2937
Browse files Browse the repository at this point in the history
BAEL-2937
  • Loading branch information
eric-martin authored May 21, 2019
2 parents 362e780 + d67dd3c commit dd160bf
Showing 1 changed file with 91 additions and 0 deletions.
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.");
}

}

0 comments on commit dd160bf

Please sign in to comment.