Skip to content

Commit

Permalink
ShutdownableThread now waits forever if await time is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
terzerm committed Jul 23, 2019
1 parent 443fe05 commit ffae33d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/tools4j/nobark/run/Joinable.java
@@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 nobark (tools4j), Marco Terzer, Anton Anufriev
* 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
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/org/tools4j/nobark/run/Shutdownable.java
Expand Up @@ -69,19 +69,17 @@ public interface Shutdownable {
boolean isShutdown();

/**
* Returns {@code true} if this service has terminated following shut down. Note that
* {@code isTerminated} is never {@code true} unless either {@code shutdown} or
* {@code shutdownNow} was called first.
* Returns {@code true} if this service has terminated.
*
* @return {@code true} if the service has terminated following shut down
* @return {@code true} if the service has terminated.
*/
boolean isTerminated();

/**
* Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs,
* whichever happens first.
* 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
* @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
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/org/tools4j/nobark/run/ShutdownableThread.java
Expand Up @@ -52,9 +52,9 @@ public class ShutdownableThread implements ThreadLike, Shutdownable {
* Constructor for shutdownable thread; it is recommended to use the static start(..) methods instead.
*
* @param mainRunnableFactory the factory for the main runnable;
* the <i>{@link #isMainRunning}</i> condition is passed to the factory as lambda
* the <i>{@link #keepMainRunning}</i> condition is passed to the factory as lambda
* @param shutdownRunnableFactory the factory for the shutdown phase runnable;
* the <i>{@link #isShutdownRunning}</i> condition is passed to the factory as lambda
* the <i>{@link #keepShutdownRunning}</i> condition is passed to the factory as lambda
* @param threadFactory the factory to provide the thread
*/
protected ShutdownableThread(final RunnableFactory mainRunnableFactory,
Expand All @@ -70,9 +70,9 @@ protected ShutdownableThread(final RunnableFactory mainRunnableFactory,
* Creates, starts and returns a new shutdownable thread.
*
* @param mainRunnableFactory the factory for the main runnable;
* the <i>{@link #isMainRunning}</i> condition is passed to the factory as lambda
* the <i>{@link #keepMainRunning}</i> condition is passed to the factory as lambda
* @param shutdownRunnableFactory the factory for the shutdown phase runnable;
* the <i>{@link #isShutdownRunning}</i> condition is passed to the factory as lambda
* the <i>{@link #keepShutdownRunning}</i> condition is passed to the factory as lambda
* @param threadFactory the factory to provide the thread
* @return the newly created and started shutdownable thread
*/
Expand All @@ -83,8 +83,8 @@ public static ShutdownableThread start(final RunnableFactory mainRunnableFactory
}

private void run() {
final Runnable main = mainRunnableFactory.create(this::isMainRunning);
final Runnable shutdown = shutdownRunnableFactory.create(this::isShutdownRunning);
final Runnable main = mainRunnableFactory.create(this::keepMainRunning);
final Runnable shutdown = shutdownRunnableFactory.create(this::keepShutdownRunning);
main.run();
shutdown.run();
}
Expand All @@ -104,11 +104,11 @@ public List<Runnable> shutdownNow() {
return Collections.emptyList();
}

private boolean isMainRunning() {
private boolean keepMainRunning() {
return (state.get() & SHUTDOWN) == 0;
}

private boolean isShutdownRunning() {
private boolean keepShutdownRunning() {
return (state.get() & SHUTDOWN_NOW) == 0;
}

Expand All @@ -119,7 +119,7 @@ public boolean isShutdown() {

@Override
public boolean isTerminated() {
return isShutdown() && threadState() == Thread.State.TERMINATED;
return threadState() == Thread.State.TERMINATED;
}

@Override
Expand All @@ -130,9 +130,6 @@ public boolean awaitTermination(final long timeout, final TimeUnit unit) {
if (isTerminated()) {
return true;
}
if (timeout == 0) {
return isTerminated();
}
final long millis = unit.toMillis(timeout);
final long nanos = unit.toNanos(timeout - unit.convert(millis, TimeUnit.MILLISECONDS));
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tools4j/nobark/run/ThreadLike.java
@@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 nobark (tools4j), Marco Terzer, Anton Anufriev
* 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tools4j/nobark/run/ThreadState.java
@@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 nobark (tools4j), Marco Terzer, Anton Anufriev
* 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tools4j/nobark/run/package-info.java
@@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 nobark (tools4j), Marco Terzer, Anton Anufriev
* 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
Expand Down
17 changes: 11 additions & 6 deletions src/test/java/org/tools4j/nobark/run/ShutdownableThreadTest.java
Expand Up @@ -55,6 +55,13 @@ private static RunnableFactory loopUntil(final BooleanSupplier loopCondition) {
};
}

private static RunnableFactory loopForMillis(final long millis) {
return run -> () -> {
final long endTime = System.currentTimeMillis() + millis;
while (System.currentTimeMillis() < endTime);
};
}

@Test
public void shutdown() {
//given
Expand Down Expand Up @@ -185,15 +192,13 @@ public void awaitTermination() {
}

@Test
public void awaitTimeout_zeroWaitsNotAtAll() {
public void awaitTimeout_zeroWaitsForever() {
//given
final Shutdownable thread = ShutdownableThread.start(LOOP_WHILE_RUNNING, LOOP_ONCE, Thread::new);
final Shutdownable thread = ShutdownableThread.start(loopForMillis(500), LOOP_ONCE, Thread::new);

//when + then
assertFalse(thread.awaitTermination(0, TimeUnit.SECONDS));
assertFalse(thread.isTerminated());

thread.shutdownNow();
assertTrue(thread.awaitTermination(0, TimeUnit.SECONDS));
assertTrue(thread.isTerminated());
}

@Test
Expand Down

0 comments on commit ffae33d

Please sign in to comment.