求教多线程问题! 运行后不停的~~~抛异常

login_user 2009-07-22 03:43:14
程序目标是想实现: 用户在控制台输入内容, 程序接收到用户的输入以后,启动一个单独的线程, 这个线程的作用是延时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();
}
}
}
}
}
...全文
168 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2009-07-23
  • 打赏
  • 举报
回复
汗一个。
楼主把那个while循环,放到try块里面把。这样,每次循环,都无法运行到finnaly块里面的代码了,除非有异常抛出。
如果楼主想捕获异常后继续运行。
那么不如把fiannly块删除。在while循环外面,while循环的后面,执行br.close();
login_user 2009-07-23
  • 打赏
  • 举报
回复
谢谢各位了,我调试的时候就觉得一定是提前关闭了流,目前采用的是5楼哥们的方法,感觉在finally里执行close()会比较好一些,如果在循环体内,可能会忘记:-)
Edwin603 2009-07-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dlnu05610 的回复:]
其实你完全可以不理会是否要关闭流,因为你的程序一直在跑,而且你没有对文件进行操作,当你的程序运行完时,你的流最终也会被GC搞定的。
[/Quote]
误人子弟 坚定完毕
不想把专业书籍在拿上来说事
close() 是必要的
你所说的gc是什么 System.gc()?
垃圾回收 和 关闭操作系统底层资源 一会事吗?
flybird 2009-07-22
  • 打赏
  • 举报
回复
最后的时候关闭流
while退出之后
Edwin603 2009-07-22
  • 打赏
  • 举报
回复
楼主 5楼和7楼的代码 都可以让你的程序正常执行
主要目的在于 你要保证当不需要read的时候 再close();这个才是你出的问题
我在5楼贴的代码意思 是你循环结束后关闭close();
7楼的代码是 当你在输入quit 后 关闭close();
只要取保不需要再读了 关闭流才行
API里解释的也很清楚:
关闭该流并释放与之关联的所有资源。在关闭该流后,再调用 read()、ready()、mark()、reset() 或 skip() 将抛出 IOException。关闭以前关闭的流无效。
dlnu05610 2009-07-22
  • 打赏
  • 举报
回复
有时候关闭流对小型的系统会存在一定的bug的
dlnu05610 2009-07-22
  • 打赏
  • 举报
回复
其实你完全可以不理会是否要关闭流,因为你的程序一直在跑,而且你没有对文件进行操作,当你的程序运行完时,你的流最终也会被GC搞定的。
南南北北 2009-07-22
  • 打赏
  • 举报
回复

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

closewbq 2009-07-22
  • 打赏
  • 举报
回复

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();
}
}
}
rascalboy520 2009-07-22
  • 打赏
  • 举报
回复

finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}


你这样写,肯定有问题的,
Edwin603 2009-07-22
  • 打赏
  • 举报
回复
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();
}
}
}
hell2010 2009-07-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 edwin603 的回复:]
哦 是你还怎么读呢
你的代码 我没运行 看看异常 又看代码
你的循环里就把流给关了 第2次循环的时候 那流都关闭了 怎么读呢
[/Quote]

同意楼上的~
Edwin603 2009-07-22
  • 打赏
  • 举报
回复
哦 是你还怎么读呢
你的代码 我没运行 看看异常 又看代码
你的循环里就把流给关了 第2次循环的时候 那流都关闭了 怎么读呢
Edwin603 2009-07-22
  • 打赏
  • 举报
回复
你把流关了 还怎么写呢
login_user 2009-07-22
  • 打赏
  • 举报
回复
贴一下控制台的信息:
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)
就是这些,重复的出现....晕了

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧