remove weird wait/notify structure in server
This commit is contained in:
parent
680c2fb8d3
commit
3f5dfabe48
1 changed files with 13 additions and 20 deletions
|
@ -26,31 +26,24 @@ public class Server {
|
|||
|
||||
public void start() {
|
||||
while (true) {
|
||||
Socket client;
|
||||
|
||||
try {
|
||||
client = this.socket.accept();
|
||||
} catch (Exception e) {
|
||||
System.out.printf("Failed to accept client:%n");
|
||||
e.printStackTrace(System.out);
|
||||
continue;
|
||||
}
|
||||
|
||||
Thread.ofVirtual().start(() -> {
|
||||
try (Socket client = this.socket.accept()) {
|
||||
synchronized (this.socket) {
|
||||
this.socket.notify();
|
||||
}
|
||||
|
||||
try (client) {
|
||||
this.handleConnection(client);
|
||||
} catch (IOException e) {
|
||||
synchronized (this.socket) {
|
||||
this.socket.notify();
|
||||
}
|
||||
|
||||
System.out.printf("Failed to handle client.%n");
|
||||
} catch (Exception e) {
|
||||
System.out.printf("Failed to handle client:%n");
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
synchronized (this.socket) {
|
||||
this.socket.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
System.out.printf("Failed to wait on server socket to accept client.%n");
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue