Skip to content

Commit 4970d40

Browse files
committed
Introduce Joinable interface; StoppableThread now implments Joinable and RunningCondition
1 parent 1cdbfc5 commit 4970d40

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 nobark (tools4j), Marco Terzer, Anton Anufriev
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.tools4j.nobark.loop;
25+
26+
/**
27+
* Joinable is a running service such as a thread that can be {@link #join() joined} to await
28+
* its termination.
29+
*/
30+
@FunctionalInterface
31+
public interface Joinable {
32+
/**
33+
* Waits for this Joinable to die.
34+
*
35+
* <p> An invocation of this method behaves in exactly the same
36+
* way as the invocation
37+
*
38+
* <blockquote>
39+
* {@linkplain #join(long) join}{@code (0)}
40+
* </blockquote>
41+
*
42+
* @throws IllegalStateException
43+
* if any thread has interrupted the current thread
44+
* @see Thread#join()
45+
*/
46+
default void join() {
47+
join(0);
48+
}
49+
50+
/**
51+
* Waits at most {@code millis} milliseconds for this Joinable to
52+
* die. A timeout of {@code 0} means to wait forever.
53+
*
54+
* @param millis
55+
* the time to wait in milliseconds
56+
* @throws IllegalArgumentException
57+
* if the value of {@code millis} is negative
58+
* @throws IllegalStateException
59+
* if any thread has interrupted the current thread
60+
* @see Thread#join(long)
61+
*/
62+
void join(long millis);
63+
}

src/main/java/org/tools4j/nobark/loop/StoppableThread.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
import sun.misc.Contended;
3030

31+
import static org.tools4j.nobark.loop.RunnableFactory.RunningCondition;
32+
3133
/**
3234
* A thread that performs a {@link java.lang.Runnable runnable} in a new thread.
3335
* The thread is started immediately upon construction and it can be stopped via stop or auto-close method.
3436
*/
35-
public class StoppableThread implements Stoppable {
37+
public class StoppableThread implements Stoppable, Joinable, RunningCondition {
3638
private final RunnableFactory runnableFactory;
3739
private final Thread thread;
3840
@Contended
@@ -65,10 +67,11 @@ public static StoppableThread start(final RunnableFactory runnableFactory, final
6567
}
6668

6769
private void run() {
68-
final Runnable runnable = runnableFactory.create(this::isRunning);
70+
final Runnable runnable = runnableFactory.create(this);
6971
runnable.run();
7072
}
7173

74+
@Override
7275
public boolean isRunning() {
7376
return running;
7477
}
@@ -78,10 +81,7 @@ public void stop() {
7881
running = false;
7982
}
8083

81-
public void join() {
82-
join(0);
83-
}
84-
84+
@Override
8585
public void join(final long millis) {
8686
try {
8787
thread.join(millis);

0 commit comments

Comments
 (0)