我想使一個服務器,可以有多個用戶,即時只創建2個線程,但我的BufferedReader.readLine()似乎是使多個線程和導致OutOfMemory異常,我不明白爲什麼它這樣做?BufferedReader.readLine()創建線程()?
功能造成例外:
public void run() {
try {
Username = Input.readLine();
} catch (IOException e1) {
disconnect();
}
String lastInput = null;
try {
while ((lastInput = Input.readLine()) != null) {
System.out.println(lastInput);
if (lastInput.startsWith("Chat: ")) {
sendToAllClients(lastInput.substring(7));
}
}
} catch (IOException e) {
disconnect();
}
}
除外:
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Main.User.run(User.java:46)
at java.lang.Thread.run(Unknown Source)
注意:用戶名= Input.readLine()被使異常
你不應該吞嚥這樣的例外...... – Sinkingpoint
我不知道你爲什麼要嘗試處理異常處理程序中的'lastInput' ...... – MadProgrammer