我在Java中有一個多線程回顯服務器的簡單代碼(它將返回的任何內容返回給客戶端)。我正在分析服務器的各種資源,包括線程統計信息。以下是按連接客戶端數量計算的一些統計數據。我的問題是基線(客戶端數量爲0)與非基線相比!守護進程線程數,線程數和總啓動線程數
1)爲什麼當一個客戶端連接時,總線程數增加2? (對於其餘部分,增加1是有意義的)
2)什麼是兩個非守護線程?爲什麼守護進程最初增加1,然後被修復?
它們是隨機的嗎?!
# clients 0 1 2 3 4 5 6 7 8 9 10
Total Started Thread Count 15 18 19 20 21 22 23 24 25 26 27
Thread count 14 16 17 18 19 20 21 22 23 24 25
Peak thread count 14 16 17 18 19 20 21 22 23 24 25
Daemon thread count 12 13 13 13 13 13 13 13 13 13 13
這是服務器的一段代碼。我正在使用RMI(用於客戶端輪詢郵件)和服務器套接字(用於客戶端發送郵件)。如果需要其他課程,請告訴我。
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
public class ServerRMI extends Thread implements Hello {
//centralized token manager runs polling server and socket server to receive updated tokens
static Vector<String> tokenList= new Vector<String>();
protected Socket clientSocket;
static int RMIRegistryPort=9001;
static int SocketServerPort=9010;
public static void main(String[] args) throws IOException {
try {
ServerRMI obj = new ServerRMI();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.createRegistry(RMIRegistryPort);
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
ServerSocket serverSocket = null;
//initialize token list
//A needs to execute first
tokenList.add(0,"0");
try {
serverSocket = new ServerSocket(SocketServerPort);
System.out.println("Connection Socket Created");
try {
while (true) {
System.out.println("Waiting for Connection");
new ServerRMI(serverSocket.accept());
}
} catch (IOException e) {
System.err.println("Accept failed.");
}
} catch (IOException e) {
System.err.println("Could not listen on port: "+SocketServerPort);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close port: "+SocketServerPort);
}
}
}
private ServerRMI(Socket clientSoc) {
clientSocket = clientSoc;
start();
}
public ServerRMI() {}{
// TODO Auto-generated constructor stub
}
public void run() {
System.out.println("New Communication Thread Started");
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
tokenList.add(0,inputLine);
System.out.println("Server received: " + inputLine);
// System.out.println(" ququ size: "+queue.size());
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("Problem with Communication Server");
}
}
public String pollServer() {
if(!tokenList.isEmpty()){
String data = tokenList.get(0);
System.out.println("Poll data: "+data);
return data;
} else{
return tokenList.size()+"";
}
}
}
郵編您接受請 – qwwdfsad
您可以檢查所使用的分析工具線程信息。例如,'jconsole'或'jvisualvm'顯示「線程」選項卡中的所有線程信息。 在這個過程中還會運行一些分析器線程,這將增加計數。 –
我們不知道你的代碼,所以我不知道我們怎麼回答。如果我是你的話,我會在#客戶端爲0時執行線程轉儲,然後當它爲1並比較它們時,你將得到你的答案 –