2013-12-09 51 views
1

我想使一个服务器,可以有多个用户,即时只创建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()被使异常

+1

你不应该吞咽这样的例外...... – Sinkingpoint

+0

我不知道你为什么要尝试处理异常处理程序中的'lastInput' ...... – MadProgrammer

回答

1

要避免无限循环,因此您的OOM例外:

try{ 
    while ((currentInput=Input.readLine()) != null) { 
    if (currentInput.startsWith("Chat: ")) 
     sendToAllClients(currentInput.substring(7)); 
    } 
catch (IOException e) { //bad to swallow exception: let's the method throw it or make something with it here} 
-1

Out of memory heap space进入画面是程序正在无限循环。

您的代码:

while (true) { 
     try { 
      lastInput = Input.readLine(); 
     } catch (IOException e) {} 
     if (lastInput != null) { 
      System.out.println(lastInput); 
      if (lastInput.startsWith("Chat: ")) { 
       sendToAllClients(lastInput.substring(7)); 
      } 
     } 

说,循环内的代码将运行无限多次,而不作为退出条件的任何条件。即使出现问题:您正捕获该异常并且代码在循环内部继续保持。

这导致Out of Memory : Heap Space.

Suggesed解决方案:

while (true) 
{ 
    try 
    { 
     lastInput = Input.readLine(); 
    } 
    catch (IOException e) 
    { 
    break; 
    } 
    if (lastInput != null) 
     { 
      System.out.println(lastInput); 
      if (lastInput.startsWith("Chat: ")) 
      { 
      sendToAllClients(lastInput.substring(7)); 
      } 
     } 
} 

这个循环将打破尽快用户输入导致异常的任何名称(事实上,作为退出条件的敌人while循环)

编辑

一个领域,我看可能是在S问题的乌尔斯河可能是:

lastInput.substring(7) 

如果lastInput字符串是规模庞大,几乎可以填补装系统,然后调用从7th character to the last character一个substringheap space of the JVM的,会在内部引发新的String创建(因为字符串是不可变的),&没有足够的剩余空间,substring执行给出OutOfMemory exception

+0

我并不积极,但我认为无限循环试图让程序保持活跃状态​​,像服务器一样工作。 –

+0

我更改了while循环,但用户名get是执行Exception的行。 – user2601014

+0

如果readLine()返回null,则此代码必须分解出来。否则它是无用的。 -1 – EJP

-1

第一:检查你的程序的while循环。 秒:设置参数集JAVA_OPTS = -Xms32m -Xmx512m。

+0

我设置了Xms和Xmx,希望它能够修复它。它的Username = Input.readLine()使得异常不在while循环中 – user2601014

+0

检查while循环如何? – EJP

0

readLine()不创建线程。

如果'lastInput'为null,则应退出循环并关闭流。

如果您遇到异常,请将其记录或打印出来,关闭流并断开。

+0

循环将在客户端发送到服务器时获取来自客户端的所有输入。所以它只是打印它。问题不是循环,它的获取用户名= Input.readLine() – user2601014

+0

当readLine()返回null时,那么*是*没有更多的输入。以后再。你可以随时尝试,而不是毫无意义地争论。如果你的代码是完美的,你就不必提问这个问题。 – EJP