我编写了一个小的TCP线程服务器,它为每个server.accept()创建一个新的线程。几乎所有的工作都很好,但是我遇到了每个中断都要杀死所有线程的问题。 (我使用ServiceExecutor来管理线程,因此我使用shutdownNow方法来访问中断方法)Worker-instances使用BufferedReader,它是readline方法来接收和计算输入。 AFAIK readline块和不会对中断发生反应,但是如何阻止它呢?关闭TCP线程服务器
while(!isInterrupted()){
try {
clientSocket = this.serverSocket.accept();
} catch(IOException e){
break;
}
this.threadPool.execute(new ThreadWorker(clientSocket));
}
threadPool.shutdownNow();
try{
serverSocket.close();
}catch(IOException e){
//todo
}
我试图关闭的ServerSocket杀输入/输出流,但如预期没有奏效。
理想情况下,不要调用无限期阻塞的方法。 –
参见http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java?rq=1和http://stackoverflow.com/questions/1948317/forceful-termination线程卡在某种方法上的某种方法?rq = 1 – DNA
在'C'中,我会说只是发送一个信号。 – alk