0
我有一個應用程序在java中正在播放服務器的rolle。爲了限制傳入連接的數量,我使用了ThreadPool服務器。理解java套接字的問題
但我理解代碼的一部分的幾個問題:
這裏爲y代碼:
protected ExecutorService threadPool =
Executors.newFixedThreadPool(5);
public ThreadPooledServer(BlockingQueue queue,int port) {
this.serverPort = port;
this.queue=queue;
}
while (!isStopped()) {
Socket clientSocket = null;
try {
System.out.println("Serverul asteapta clienti spre conectare la port" +serverPort);
clientSocket = serverSocket.accept();
clientconnection++;
System.out.println("Serverul a acceptat clientul cu numarul:"
+ clientconnection);
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.");
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);
this.threadPool.execute(workerRunnable);
}
this.threadPool.shutdown();
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
}
catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
} catch (IOException e) {
throw new RuntimeException("Cannot open port", e);
}
}
什麼,我不明白:
我正在使用一個ThreadPooledServer,它的acce對於5個傳入連接的點...
與客戶端的連接在while()循環中完成。
while (!isStopped()) {
}
isStopped是BYT此函數返回一個布爾變量:
private synchronized boolean isStopped() {
return this.isStopped;
}
我稱之爲作爲用於開始循環的條件。
這個布爾變量最初設置爲false .....並重新設置爲true,在這裏:
public synchronized void stop() {
this.isStopped = true;
}
當是設置回真我的同時()循環結束,然後我關閉我的線程池中的所有工作人員。
this.threadPool.shutdown();
的問題是,我從來沒有調用這個函數「停止()」
問:當我關閉自動調用該函數我的服務器????? ......或者我應該叫爲它的某個地方?