我有一封信,聆听了特定东道国名称和港口的连接。 听取意见时使用以下方法:listen(
)(见下文),该方法不断等待使用<代码>ServerSocket.accept()的新链接,创建新的<代码>Thread,处理投入流。
private ServerSocket serverSocket;
private Thread listenerThread;
public void listen() throws IOException {
this.listenerThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Socket socket = TheServerClass.this.serverSocket.accept();
// Create new thread to handle the incoming connection
}
catch (IOException exc) { }
}
}
});
this.listenerThread.start();
}
Now I want to stop the running of listenerThread
. But when I call this.listenerThread.interrupt()
, this doesn t work.
I thought you can stop a thread by interrupting it, so why isn t that working?
(Notice: 一种可能的解决办法是使用<代码>ServerSocket 关闭interrupt(
或某种方式完成?)