java中的管道和线程问题,大虾来帮忙啊:)

cloud_soft 2003-09-16 11:19:13
偶写地代码如下,为什么线程停不下来啊?
import java.io.*;

public class PipedTest
{
public static void main(String[] args)
{
try
{
PipedInputStream pisIn = new PipedInputStream();
PipedOutputStream posOut = new PipedOutputStream();
posOut.connect(pisIn);
Sender objSend = new Sender(posOut,args[0]);
Receiver objRec = new Receiver(pisIn);

objSend.start();
objRec.start();

}
catch(IOException e)
{
System.out.println("Error !"+e);
}
}
}

class Sender extends Thread
{
PipedOutputStream posOut;
File file;
Sender(PipedOutputStream posOut,String file)
{
this.posOut = posOut;
this.file = new File(file);
}
public void run()
{
try
{
FileInputStream fs = new FileInputStream(file);
int data;
data = fs.read();
while(data!=-1)
{
posOut.write(data);
data=fs.read();
}
posOut.close();
}
catch(IOException e)
{
System.out.println("sender error"+e);
}
}
}
class Receiver extends Thread
{
PipedInputStream pisIn;
Receiver(PipedInputStream pisIn)
{
this.pisIn = pisIn;
}

public void run()
{
try
{
int data;
int intTmp=0;
StringBuffer objStr = new StringBuffer();
data=pisIn.read();
while(data!=-1)
{
intTmp++;
if(intTmp%16==0) objStr.append(data+"\n");
objStr.append(data);
System.out.println(objStr);
data=pisIn.read();

}
pisIn.close();
}
catch(IOException e)
{
System.out.println("Receiver Error!"+e);
}
}
}


...全文
20 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
阎罗 2003-09-16
  • 打赏
  • 举报
回复
哈哈,解决了就好,给你up一下
cloud_soft 2003-09-16
  • 打赏
  • 举报
回复
ft阿ft,代码错误,自己解决了:)

62,614

社区成员

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

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