-
Notifications
You must be signed in to change notification settings - Fork 10
/
Shutdownable.java
89 lines (82 loc) · 3.62 KB
/
Shutdownable.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 nobark (tools4j), Marco Terzer, Anton Anufriev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.tools4j.nobark.run;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import org.tools4j.nobark.loop.Step;
/**
* Shutdownable is a running service such as a thread that can be shutdown orderly or abruptly in a way similar to
* {@link ExecutorService}.
*/
public interface Shutdownable {
/**
* Initiates an orderly shutdown in which the service will be continue to perform its duties
* as long as there is work to be done. For instance for a sequence of steps, the service could
* continue to invoke the steps' {@link Step#perform() perform()} methods until all invocations
* yield false (indicating that no work was done).
* <p>
* This method does not wait for for termination; use {@link #awaitTermination awaitTermination}
* to do that.
* <p>
* Invocation has no additional effect if already shut down.
*/
void shutdown();
/**
* Initiates an immediate shutdown in which the service will stop even if there is more work
* to be done. For instance for a sequence of steps, the service could abort invocation of the
* steps' {@link Step#perform() perform()} irrespective of prior invocation results.
* <p>
* This method does not wait for for termination; use {@link #awaitTermination awaitTermination}
* to do that.
* <p>
* Invocation has no additional effect if already shut down.
*
* @return list of tasks that never commenced execution if any, otherwise an empty list
*/
List<Runnable> shutdownNow();
/**
* Returns {@code true} if this service has been shut down.
*
* @return {@code true} if this service has been shut down
*/
boolean isShutdown();
/**
* Returns {@code true} if this service has terminated.
*
* @return {@code true} if the service has terminated.
*/
boolean isTerminated();
/**
* Blocks until this service has terminated, or the timeout occurs, whichever happens first.
* Zero timeout means waiting forever.
*
* @param timeout the maximum time to wait, zero to wait indefinitely
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws IllegalStateException if any thread has interrupted the current thread
*/
boolean awaitTermination(long timeout, TimeUnit unit);
}