Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class EmulatorController {

private final Path executable;
private Process process;
private Thread stdoutThread;
private Thread stderrThread;
private boolean isStopped = true;
private Thread shutdownHook;

Expand Down Expand Up @@ -127,8 +129,8 @@ public synchronized void start(int port)
throw e;
}
}
pipeStreamToLog(process.getInputStream(), Level.INFO);
pipeStreamToLog(process.getErrorStream(), Level.WARNING);
Thread stdoutThread = pipeStreamToLog(process.getInputStream(), Level.INFO);
Thread stderrThread = pipeStreamToLog(process.getErrorStream(), Level.WARNING);
isStopped = false;

shutdownHook =
Expand Down Expand Up @@ -164,6 +166,23 @@ public synchronized void stop() {
} finally {
isStopped = true;
process.destroy();

try {
process.waitFor();
if (stdoutThread != null) {
stdoutThread.join();
}
if (stderrThread != null) {
stderrThread.join();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.log(Level.WARNING, "Interrupted while waiting for emulator to stop", e);
} finally {
stdoutThread = null;
stderrThread = null;
process = null;
}
}
}

Expand Down Expand Up @@ -239,7 +258,7 @@ private static void waitForPort(int port) throws InterruptedException, TimeoutEx
}

/** Creates a thread that will pipe an {@link InputStream} to this class' Logger. */
private static void pipeStreamToLog(final InputStream stream, final Level level) {
private static Thread pipeStreamToLog(final InputStream stream, final Level level) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

Thread thread =
Expand All @@ -258,6 +277,7 @@ private static void pipeStreamToLog(final InputStream stream, final Level level)
});
thread.setDaemon(true);
thread.start();
return thread;
}
// </editor-fold>
}
Loading