日期:2014-05-20  浏览次数:20880 次

求教多线程问题! 运行后不停的~~~抛异常
程序目标是想实现: 用户在控制台输入内容, 程序接收到用户的输入以后,启动一个单独的线程, 这个线程的作用是延时10秒把这个内容回显到控制台, 在这期间用户可继续输入内容, 每次输入都启动一个线程.

可是运行时, 当我输入第一个内容,回车后,控制台就开始不停(对!~是不停...)的抛Stream closed的Exception, 可是我并没有退出循环, 怎么会执行br.close()呢?

以下是程序的两个类的代码

Java code
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();
        } 
    }
}


Java code
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();
                }
            }
        }
    }
}



------解决方案--------------------
你把流关了 还怎么写呢
------解决方案--------------------
Java code

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();
            }
        }
    }

------解决方案--------------------
Java code

 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();
                }
            }
        }
    }