Refactor: Moving out methods for clarity.

This commit is contained in:
Samuel Carlsson 2016-08-02 21:52:34 +02:00
parent ce6f04bdc7
commit c2acf534ff

View File

@ -22,11 +22,7 @@ public abstract class SocketServer implements Runnable {
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
synchronized (lockObject) {
if (!isStarted) {
lockObject.wait();
}
}
serverReady();
}
public int getPort() {
@ -39,10 +35,7 @@ public abstract class SocketServer implements Runnable {
socket = new ServerSocket(port);
socket.setReuseAddress(true);
synchronized (lockObject) {
lockObject.notify();
isStarted = true;
}
waitForServer();
while (true) {
Socket c = socket.accept();
@ -54,6 +47,21 @@ public abstract class SocketServer implements Runnable {
}
}
private void serverReady() throws InterruptedException {
synchronized (lockObject) {
if (!isStarted) {
lockObject.wait();
}
}
}
private void waitForServer() {
synchronized (lockObject) {
lockObject.notify();
isStarted = true;
}
}
protected abstract Runnable createResponder(Socket socket);
public void stop() throws IOException, InterruptedException {