程序目标是想实现: 用户在控制台输入内容, 程序接收到用户的输入以后,启动一个单独的线程, 这个线程的作用是延时10秒把这个内容回显到控制台, 在这期间用户可继续输入内容, 每次输入都启动一个线程.可是运行时, 当我输入第一个内容,回车后,控制台就开始不停(对!~是不停...)的抛Stream closed的Exception, 可是我并没有退出循环, 怎么会执行br.close()呢?以下是程序的两个类的代码package net;public class NetThread implements Runnable{
String data = null;

//constructor
public NetThread(String data){
this.data = data;
}

//run method
public void run(){
try {
System.out.println("Start sending...");
Thread.sleep(10000); //delay time
System.out.println("Sending completely! The message is: " + data);
} catch (InterruptedException e) {
e.printStackTrace();

}
}package net;import java.io.*;public class Test {
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;

while(true){
try {
System.out.println("Please enter: ");
input = br.readLine(); //accept input from console
if(input.equals("quit")){
System.out.println("Quit successfully!");
break; // exit
}

//create and start sending thread
Thread nt = new Thread(new NetThread(input));
nt.start();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

解决方案 »

  1.   

    贴一下控制台的信息:
    Please enter: 
    at net.Test.main(Test.java:13)
    java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at net.Test.main(Test.java:13)
    就是这些,重复的出现....晕了
      

  2.   

    import java.io.*;public class Test1 {
        public static void main(String[] args){
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String input = null;
            
            while(true){    
                try {
                    System.out.println("Please enter: ");
                    input = br.readLine(); //accept input from console
                    if(input.equals("quit")){
                        System.out.println("Quit successfully!");
                        break; // exit
                    }
                    
                    //create and start sending thread
                    Thread nt = new Thread(new NetThread(input));
                    nt.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    
                
            }
    //把这句放到循环外面去 你再运行!
            try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
        }
    }
      

  3.   


    finally{
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    你这样写,肯定有问题的,
      

  4.   


    public static void main(String[] args){
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String input = null;
            
            while(true){    
                try {
                    System.out.println("Please enter: ");
                    input = br.readLine(); //accept input from console
                    if(input.equals("quit")){
                        System.out.println("Quit successfully!");
                        br.close();
                        break; // exit
                    }
                    
                    //create and start sending thread
                    Thread nt = new Thread(new NetThread(input));
                    nt.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
      

  5.   


     public static void main(String[] args){
           
            
            while(true){   
                 //放这试下吧。 
                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 String input = null;
                try {
                    System.out.println("Please enter: ");
                    input = br.readLine(); //accept input from console
                    if(input.equals("quit")){
                        System.out.println("Quit successfully!");
                        break; // exit
                    }
                    
                    //create and start sending thread
                    Thread nt = new Thread(new NetThread(input));
                    nt.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
      

  6.   

    有时候关闭流对小型的系统会存在一定的bug的
      

  7.   

    楼主  5楼和7楼的代码 都可以让你的程序正常执行
    主要目的在于 你要保证当不需要read的时候 再close();这个才是你出的问题
    我在5楼贴的代码意思 是你循环结束后关闭close();
    7楼的代码是 当你在输入quit 后 关闭close();
    只要取保不需要再读了 关闭流才行
      API里解释的也很清楚:
    关闭该流并释放与之关联的所有资源。在关闭该流后,再调用 read()、ready()、()、reset() 或 skip() 将抛出 IOException。关闭以前关闭的流无效。
      

  8.   

    最后的时候关闭流
    while退出之后
      

  9.   

    误人子弟 坚定完毕
    不想把专业书籍在拿上来说事
    close() 是必要的
    你所说的gc是什么 System.gc()?
    垃圾回收 和 关闭操作系统底层资源 一会事吗?
      

  10.   

    汗一个。
    楼主把那个while循环,放到try块里面把。这样,每次循环,都无法运行到finnaly块里面的代码了,除非有异常抛出。
    如果楼主想捕获异常后继续运行。
    那么不如把fiannly块删除。在while循环外面,while循环的后面,执行br.close();
      

  11.   

    谢谢各位了,我调试的时候就觉得一定是提前关闭了流,目前采用的是5楼哥们的方法,感觉在finally里执行close()会比较好一些,如果在循环体内,可能会忘记:-)