现在的程序一旦client端连接上接受了数据,CPU使用率就达到100%,如何解决?class ServeOneJabber extends Thread   { private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static String Get = "get";
private static String Set = "set";

Timer timer = new Timer();

public ServeOneJabber(Socket s) throws IOException {
 socket = s;
     in =  new BufferedReader(new InputStreamReader(socket.getInputStream()));
     out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);
 start(); // Calls run()
} //doWithGet
public String doWithGet(String contentText) { }

//doWithSet
public String doWithSet(String contentText) {
    
}

public void run() {
    //StringBuffer allContent = new StringBuffer("");
    String  allContent;
     char[] cIn = new char[4096];
     int rInt;     
 try {
   while (true) {  
     if (socket.getInputStream().available() <= 0) {
         //System.out.println("Waiting for data");
         continue;
     }
     
     rInt = in.read(cIn,0,4096);
     allContent = new String(cIn,0,rInt);
     System.out.println("receive...");
     System.out.println(allContent);
                 
.....
    
             
     //send
     System.out.println("begin to send");
     out.print(sendBackAll);
     out.flush();
     System.out.println(allContent);    }
   //out.println("closing...");
 } catch (IOException e) {
 } finally {
   try {
     socket.close();
   } catch(IOException e) {}
 }
}
}

public class MultiJabberServer {
    
static final int PORT = 8101;

public static void main(String[] args) throws IOException {
    
 InetAddress myaddr = InetAddress.getLocalHost();
 ServerSocket s = new ServerSocket(PORT,5, myaddr);
 Socket socket = null;
 //Set TimeOut:5 seconds
 s.setSoTimeout(5000);
 s.setReuseAddress(true);
 System.out.println("Server Started");

 try {
   
   while(true) {
     // Blocks until a connection occurs:
     try {
         socket = s.accept();
         System.out.println("receive one connection!");
         new ServeOneJabber(socket);          
                    
     } catch(SocketTimeoutException e) {
         System.out.println("Time Out");
     } catch(IOException e) {
       socket.close();
     }
   }
 } finally {
   s.close();
 }

} ///:~

解决方案 »

  1.   

    socket = s.accept();
    不要放在主线程里
      

  2.   

    while (true)线程轮巡太平凡,试试一次轮巡后加一些等待时间
      

  3.   

    每一个请求就创建一个线程啊,又没有线程管理!
    if (socket.getInputStream().available() <= 0) {
        //System.out.println("Waiting for data");
        continue;
    }
    这段代码估计也不能保证丢数据包的问题! 没有任何作用的说!
      

  4.   

       while (true) {  
         if (socket.getInputStream().available() <= 0) {
             //System.out.println("Waiting for data");
             continue;
         }
         
         rInt = in.read(cIn,0,4096);-----------------------------------
         if (socket.getInputStream().available() <= 0) {
             //System.out.println("Waiting for data");
             continue;
         }
    出现循环了快速循环,消耗CPU,
    in.read(cIn,0,4096);是阻塞读,上面的判定没有意义,反倒引起了CPU处理的大量消耗