黑马程序员技术交流社区
标题:
求助 代码问题
[打印本页]
作者:
莫运飞
时间:
2012-4-10 13:38
标题:
求助 代码问题
import java.io.*;
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in=in;
}
public void run()
{
try
{
byte[] b=new byte[1024];
int len=0;
while((len=in.read())!=-1)
{
String s=new String(b,0,len);
System.out.println(s);
in.close();
}
}
catch(IOException e)
{
throw new RuntimeException("管道堵塞,读取失败");
}
}
}
class Writer implements Runnable
{
private PipedOutputStream out;
Writer(PipedOutputStream out)
{
this.out=out;
}
public void run()
{
try
{
out.write("飞吧".getBytes());
out.close();
}
catch(IOException e)
{
throw new RuntimeException("管道堵塞,写入失败");
}
}
}
class TestDemo
{
public static void main(String[]args)throws IOException
{
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
in.connect(out);
Read r=new Read(in);
Writer w=new Writer(out);
Thread t=new Thread(r);
Thread t1=new Thread(w);
t.start();
t1.start();
}
}
程序为什么编译通过了,但运行失败了呢??求解决。
11.png
(5.97 KB, 下载次数: 57)
下载附件
2012-4-10 13:38 上传
作者:
袁计艳
时间:
2012-4-10 14:21
是这样的:
我在你的管道输入流的那块 try catch 中 在catch的部分 打印了 异常的信息 e.getMessage();
得到了 信息为:Pipe closed 管道关闭
所以 应该把 输入流线程中的 in.close();去掉
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2