Stopping a Runnable while waiting for a connection in a while loop
I have a Runnable class which is waiting for a Socket connection. I want
to add JButton that can cause it to stop waiting for the connection and
come out of the loop.
Here is the loop
volatile boolean finished = false;
while (true && !finished) {
System.out.println("Server Started....");
clientSocket = serverSocket.accept(); // want to skip this line
when the button is pressed
if (!clientSocket.isClosed() && ServerSettings.getServerStatus() != -1) {
// start communication
} else {
// close connection
}
}
I looked for this issue and found a solution here to exit the loop. But
this does not completely solve my problem. I can change the value of
finished variable, but to check the new value I still need to skip the
waiting once.
Any help is appreciable.
No comments:
Post a Comment