mirror of
https://github.com/revanced/jadb.git
synced 2025-02-14 02:56:48 +01:00
Merge pull request #32 from vidstige/race-condition-in-socketserver
Race condition in socketserver
This commit is contained in:
commit
a01ce1bb17
@ -10,6 +10,8 @@ public abstract class SocketServer implements Runnable {
|
||||
private final int port;
|
||||
private ServerSocket socket;
|
||||
private Thread thread;
|
||||
|
||||
private boolean isStarted = false;
|
||||
private final Object lockObject = new Object();
|
||||
|
||||
protected SocketServer(int port) {
|
||||
@ -20,9 +22,7 @@ public abstract class SocketServer implements Runnable {
|
||||
thread = new Thread(this, "Fake Adb Server");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
synchronized (lockObject) {
|
||||
lockObject.wait();
|
||||
}
|
||||
serverReady();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
@ -35,9 +35,7 @@ public abstract class SocketServer implements Runnable {
|
||||
socket = new ServerSocket(port);
|
||||
socket.setReuseAddress(true);
|
||||
|
||||
synchronized (lockObject) {
|
||||
lockObject.notify();
|
||||
}
|
||||
waitForServer();
|
||||
|
||||
while (true) {
|
||||
Socket c = socket.accept();
|
||||
@ -49,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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user