I have a client <-> server application. The problem is when I click a button "Exit" it does not kill the entire thread. It kills the process, but the thread still remains active. But if I click the "X" button the JOptionPane.showConfirmDialog is stopping everything, so I would like the button to do the same (to be terminated).
P.P. The "Exit" button starts a JSwingWorker which is calling disconnect()
@Override
public void run() {
synchronized (this) {
while (!serverDownAfterTry && !serverClosedByButton) {
multicastSocket = connectToMulticastAddress(multicastIP);
mediator = new Mediator(multicastSocket, this, objectOutput, userName);
mediator.setSocketIP(clientSocket.getInetAddress().toString());
mediator.setSocketPort(clientSocket.getPort());
String input = "";
if (connected) {
mediator.writeOnMulticastAddress("USER_CONNECTED " + this.userName);
mediator.executeCommands("SYSTEM_MESSAGE " + this.userName, clientGUI);
input = mediator.listenToMultiCast(multicastSocket);
}
while (!multicastSocket.isClosed()) {
input = mediator.listenToMultiCast(multicastSocket);
if (input != null) {
mediator.executeCommands(input, clientGUI);
}
}
}
}
}
public void disconnect() {
clientGUI.dispose();
serverClosedByButton = true;
multicastSocket.close();
try {
objectOutput.close();
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} This is what happens when i click Exit button: enter image description here
This is what happens when I click "X" button on JFrame(terminated): enter image description here
When you use Swing, you need to terminate your VM to exit. For example by using of System.exit(0);