Fixed thread surviving the application lifespan.

+tests in swt.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2016-12-03 20:18:20 -05:00
parent 24f1fba97e
commit 543b1ef605
9 changed files with 179 additions and 144 deletions

View File

@@ -48,7 +48,7 @@ import java.io.InterruptedIOException;
* a message, in that case, the usual behavior is to prompt normally.
*
* @author Emmanuel BIGEON */
public interface ConsoleManager {
public interface ConsoleManager extends AutoCloseable {
/** @return the prompt prefix */
String getPrompt();
@@ -89,6 +89,7 @@ public interface ConsoleManager {
/** Closes the manager.
*
* @throws IOException if the close raised an exception */
@Override
void close() throws IOException;
/** @return if the manager is closed. */

View File

@@ -54,7 +54,7 @@ import java.nio.charset.Charset;
*
* @author Emmanuel Bigeon */
public final class PipedConsoleManager
implements ConsoleManager, AutoCloseable {
implements ConsoleManager {
/** The encoding between streams */
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
@@ -91,6 +91,7 @@ public final class PipedConsoleManager
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
th.start();
th = new Thread(reading, "read"); //$NON-NLS-1$
th.setDaemon(true);
th.start();
}

View File

@@ -88,6 +88,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
this.in = new BufferedReader(new InputStreamReader(in, charset));
reading = new ReadingRunnable(this.in);
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
promptThread.setDaemon(true);
promptThread.start();
}
@@ -156,6 +157,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
public void close() throws IOException {
closed = true;
reading.setRunning(false);
promptThread.interrupt();
}
/* (non-Javadoc)

View File

@@ -58,7 +58,7 @@ public class AOutputForwardRunnableTest {
* @author Emmanuel Bigeon */
private final class AOutputForwardTestRunnable
extends AOutputForwardRunnable {
private int count = 1;
private int count = 2;
private String message;
/** @param manager */
@@ -117,11 +117,20 @@ public class AOutputForwardRunnableTest {
manager);
Thread th = new Thread(runnable, "forward");
th.start();
manager.println("before");
th.start();
assertEquals("before", runnable.getMessage());
synchronized (this) {
wait(3000);
}
manager.println("after");
assertEquals("after", runnable.getMessage());
synchronized (this) {
wait(3000);
}
th.join();
} catch (IOException | InterruptedException e) {