【发布时间】:2013-09-28 14:31:15
【问题描述】:
我有一个Runnable 类,它正在等待Socket 连接。我想添加JButton,这会导致它停止等待连接并退出循环。
这是循环
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。
-
@Quirliom:谢谢,我已经更新了帖子。
标签: java multithreading sockets loops