现在的程序一旦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";
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();
}
}
} ///:~
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static String Get = "get";
private static String Set = "set";
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();
}
}
} ///:~
if (socket.getInputStream().available() <= 0) {
//System.out.println("Waiting for data");
continue;
}
你这里如果没有客户端的字符串发过来不就是一直死循环嘛?
cpu一定是100%,呵呵
2.你的线程run方法中没有 sleep 最起码也要 sleep(5) 以给其它线程执行的机会
对于while循环是正确的.你可以尝试一下看看