现在的程序一旦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();
}
}
} ///:~
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();
}
}
} ///:~
解决方案 »
- 请问这是怎么错了啊?
- invokeLater的调用顺序问题。比较简单的程序
- 扫雷计算周围雷的代码.. 哪里有错误..大家看看.算的不是很对.. 只有有些地方是对的.
- 菜鸟求教啥叫未经检查或不安全的操作?
- 俺新来的,问个简单问题,怎么样才可以实现用JAVA向文本中插入文字的操作.
- 一个数组引用的问题!
- (.)(.) File类中有获得给定目录的上级目录名的方法吗?
- 再发一帖,有关class文件打包问题
- 诚心求教(在线等),关于javac编译的问题,急!!!!!!!
- WEBLOGIC SERVER与WEBLOGIC有什么不同?
- 怎么才能比较时间 取得的本地时间与nyr进行比较
- 一个关于文件操作的问题,高手指教!
不要放在主线程里
if (socket.getInputStream().available() <= 0) {
//System.out.println("Waiting for data");
continue;
}
这段代码估计也不能保证丢数据包的问题! 没有任何作用的说!
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处理的大量消耗