【发布时间】:2013-06-14 02:53:13
【问题描述】:
所以,这就是我所拥有的。这是一个使用线程连接到多个客户端的服务器程序。到目前为止,该主循环几乎是无限的。 假设客户端向 ServerThread 发送了关闭命令。那个 ServerThread 是否能够访问主类,跳出循环,到达程序的末尾?
我尝试在 ServerThread 中设置 isRunning = false,但这似乎不起作用。
public class Server
{
public static boolean isRunning = true;
public static void main(String[] args)
{
// init stuff
try {
serverSocket = new ServerSocket(27647);
} catch (IOException e) {
println("Could not listen on port 27647");
}
while(isRunning)
{
Socket clientSocket = null;
try{
clientSocket = serverSocket.accept();
} catch(IOException e) {
println("Could not connect to client");
}
ServerThread serv = new ServerThread(clientSocket);
serv.start();
}
try {
serverSocket.close();
} catch (IOException e1) { }
}
}
【问题讨论】:
-
显示你在ServerThread中放置
isRunning = false的代码
标签: java multithreading loops