-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduledDelayExecutor.java
More file actions
31 lines (24 loc) · 1.01 KB
/
ScheduledDelayExecutor.java
File metadata and controls
31 lines (24 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ScheduledDelayExecutor {
public static void main(String[] args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
System.out.println("Scheduling start: " + System.nanoTime());
// schedules a task to run after an initial delay of three seconds has passed
ScheduledFuture<?> future = executor.schedule(task, 3, TimeUnit.SECONDS);
try {
TimeUnit.MILLISECONDS.sleep(1337);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//to retrieve the remaining delay time
long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
System.out.printf("Remaining Delay: %sms\n", remainingDelay);
//Will wait for the task end to shutdown
executor.shutdown();
}
}