1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.concurrency.ScheduledExecutorService;
13 
14 import hunt.concurrency.ExecutorService;
15 
16 import core.time;
17 
18 /**
19  * An {@link ExecutorService} that can schedule commands to run after a given
20  * delay, or to execute periodically.
21  *
22  * <p>The {@code schedule} methods create tasks with various delays
23  * and return a task object that can be used to cancel or check
24  * execution. The {@code scheduleAtFixedRate} and
25  * {@code scheduleWithFixedDelay} methods create and execute tasks
26  * that run periodically until cancelled.
27  *
28  * <p>Commands submitted using the {@link Executor#execute(Runnable)}
29  * and {@link ExecutorService} {@code submit} methods are scheduled
30  * with a requested delay of zero. Zero and negative delays (but not
31  * periods) are also allowed in {@code schedule} methods, and are
32  * treated as requests for immediate execution.
33  *
34  * <p>All {@code schedule} methods accept <em>relative</em> delays and
35  * periods as arguments, not absolute times or dates. It is a simple
36  * matter to transform an absolute time represented as a {@link
37  * java.util.Date} to the required form. For example, to schedule at
38  * a certain future {@code date}, you can use: {@code schedule(task,
39  * date.getTime() - System.currentTimeMillis(),
40  * TimeUnit.MILLISECONDS)}. Beware however that expiration of a
41  * relative delay need not coincide with the current {@code Date} at
42  * which the task is enabled due to network time synchronization
43  * protocols, clock drift, or other factors.
44  *
45  * <p>The {@link Executors} class provides convenient factory methods for
46  * the ScheduledExecutorService implementations provided in this package.
47  *
48  * <h3>Usage Example</h3>
49  *
50  * Here is a class with a method that sets up a ScheduledExecutorService
51  * to beep every ten seconds for an hour:
52  *
53  * <pre> {@code
54  * import static hunt.concurrency.TimeUnit.*;
55  * class BeeperControl {
56  *   private final ScheduledExecutorService scheduler =
57  *     Executors.newScheduledThreadPool(1);
58  *
59  *   public void beepForAnHour() {
60  *     Runnable beeper = () -> System.out.println("beep");
61  *     ScheduledFuture<?> beeperHandle =
62  *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
63  *     Runnable canceller = () -> beeperHandle.cancel(false);
64  *     scheduler.schedule(canceller, 1, HOURS);
65  *   }
66  * }}</pre>
67  *
68  * @since 1.5
69  * @author Doug Lea
70  */
71 interface ScheduledExecutorService : ExecutorService {
72 
73     // /**
74     //  * Submits a one-shot task that becomes enabled after the given delay.
75     //  *
76     //  * @param command the task to execute
77     //  * @param delay the time from now to delay execution
78     //  * @param unit the time unit of the delay parameter
79     //  * @return a ScheduledFuture representing pending completion of
80     //  *         the task and whose {@code get()} method will return
81     //  *         {@code null} upon completion
82     //  * @throws RejectedExecutionException if the task cannot be
83     //  *         scheduled for execution
84     //  * @throws NullPointerException if command or unit is null
85     //  */
86     // ScheduledFuture<?> schedule(Runnable command, Duration delay);
87 
88     // /**
89     //  * Submits a value-returning one-shot task that becomes enabled
90     //  * after the given delay.
91     //  *
92     //  * @param callable the function to execute
93     //  * @param delay the time from now to delay execution
94     //  * @param unit the time unit of the delay parameter
95     //  * @param (V) the type of the callable's result
96     //  * @return a ScheduledFuture that can be used to extract result or cancel
97     //  * @throws RejectedExecutionException if the task cannot be
98     //  *         scheduled for execution
99     //  * @throws NullPointerException if callable or unit is null
100     //  */
101     // !(V) ScheduledFuture!(V) schedule(Callable!(V) callable, Duration delay);
102 
103     // /**
104     //  * Submits a periodic action that becomes enabled first after the
105     //  * given initial delay, and subsequently with the given period;
106     //  * that is, executions will commence after
107     //  * {@code initialDelay}, then {@code initialDelay + period}, then
108     //  * {@code initialDelay + 2 * period}, and so on.
109     //  *
110     //  * <p>The sequence of task executions continues indefinitely until
111     //  * one of the following exceptional completions occur:
112     //  * <ul>
113     //  * <li>The task is {@linkplain Future#cancel explicitly cancelled}
114     //  * via the returned future.
115     //  * <li>The executor terminates, also resulting in task cancellation.
116     //  * <li>An execution of the task throws an exception.  In this case
117     //  * calling {@link Future#get() get} on the returned future will throw
118     //  * {@link ExecutionException}, holding the exception as its cause.
119     //  * </ul>
120     //  * Subsequent executions are suppressed.  Subsequent calls to
121     //  * {@link Future#isDone isDone()} on the returned future will
122     //  * return {@code true}.
123     //  *
124     //  * <p>If any execution of this task takes longer than its period, then
125     //  * subsequent executions may start late, but will not concurrently
126     //  * execute.
127     //  *
128     //  * @param command the task to execute
129     //  * @param initialDelay the time to delay first execution
130     //  * @param period the period between successive executions
131     //  * @param unit the time unit of the initialDelay and period parameters
132     //  * @return a ScheduledFuture representing pending completion of
133     //  *         the series of repeated tasks.  The future's {@link
134     //  *         Future#get() get()} method will never return normally,
135     //  *         and will throw an exception upon task cancellation or
136     //  *         abnormal termination of a task execution.
137     //  * @throws RejectedExecutionException if the task cannot be
138     //  *         scheduled for execution
139     //  * @throws NullPointerException if command or unit is null
140     //  * @throws IllegalArgumentException if period less than or equal to zero
141     //  */
142     // ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
143     //                                               long initialDelay,
144     //                                               Duration period);
145 
146     // /**
147     //  * Submits a periodic action that becomes enabled first after the
148     //  * given initial delay, and subsequently with the given delay
149     //  * between the termination of one execution and the commencement of
150     //  * the next.
151     //  *
152     //  * <p>The sequence of task executions continues indefinitely until
153     //  * one of the following exceptional completions occur:
154     //  * <ul>
155     //  * <li>The task is {@linkplain Future#cancel explicitly cancelled}
156     //  * via the returned future.
157     //  * <li>The executor terminates, also resulting in task cancellation.
158     //  * <li>An execution of the task throws an exception.  In this case
159     //  * calling {@link Future#get() get} on the returned future will throw
160     //  * {@link ExecutionException}, holding the exception as its cause.
161     //  * </ul>
162     //  * Subsequent executions are suppressed.  Subsequent calls to
163     //  * {@link Future#isDone isDone()} on the returned future will
164     //  * return {@code true}.
165     //  *
166     //  * @param command the task to execute
167     //  * @param initialDelay the time to delay first execution
168     //  * @param delay the delay between the termination of one
169     //  * execution and the commencement of the next
170     //  * @param unit the time unit of the initialDelay and delay parameters
171     //  * @return a ScheduledFuture representing pending completion of
172     //  *         the series of repeated tasks.  The future's {@link
173     //  *         Future#get() get()} method will never return normally,
174     //  *         and will throw an exception upon task cancellation or
175     //  *         abnormal termination of a task execution.
176     //  * @throws RejectedExecutionException if the task cannot be
177     //  *         scheduled for execution
178     //  * @throws NullPointerException if command or unit is null
179     //  * @throws IllegalArgumentException if delay less than or equal to zero
180     //  */
181     // ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
182     //                                                  long initialDelay,
183     //                                                  long delay, 
184     //                                                  Duration delay);
185 
186 }