我有一個Runnable
類,它正在等待Socket
連接。我想添加JButton
,導致它停止等待連接並退出循環。在while循環中等待連接時停止Runnable
這裏是環
volatile boolean finished = false;
while (!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
}
}
我找了這個問題,並發現a solution here退出循環。但是這並不能完全解決我的問題。我可以更改finished
變量的值,但爲了檢查新值,我仍然需要跳過等待一次。
任何幫助是可觀的。
順便說一句:true &&!finished ==!finished。 – Sinkingpoint
@Quirliom:謝謝,我更新了這篇文章。 –