我想关闭HTTP 线程池服务器。为此,我制作了一个正在监听 STDIN 的线程。如果我键入shutdown
,它会关闭服务器。
代码是
public class WebServerShutdown extends Thread{
Logger log = Logger.getLogger( WebServerShutdown.class.getName() );
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//ThreadPool threadpool = new ThreadPool();
String s;
while ((s = in.readLine()) != null && s.length() != 0) {
System.out.println(s);
if(s.equalsIgnoreCase("shutdown")) {
ThreadPool.isStopped = true;
break;
}
}
} catch (IOException ex) {
log.info(new Date() + " Problem in reading Input from keyboard");
}
System.out.println("Thread comes out");
}
}
服务器监听的代码
while( !(ThreadPool.isStopped) ) {
Socket client_socket = null;
try {
System.out.println("try to accept socket");
client_socket = serversocket.accept();
} catch(IOException ie) {
log.info(new Date() + " Server Stopped");
}
ThreadPool.threads.execute(new Worker(client_socket));
}
System.out.println("Reached here");
ThreadPool.threads.shutdown();
serversocket.close();
log.info(new Date() + " WEBSERVER STOPPED");
但是有一个问题,当我写关闭时,当时服务器进入循环并监听客户端的请求。因此,当我发出任何客户端请求时,它会关闭服务器。
但这并不好。我希望每当我写关闭时,它都不需要任何其他客户端请求。
帮助我使上面的代码正确。