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