【发布时间】:2016-02-04 01:41:07
【问题描述】:
我正在尝试使用在 2 个线程之间进行通信的管道创建一个程序(您可以说 2 个线程之间的聊天),我的问题是当您编写时没有问题,但是当您从管道中读取时,它会抛出结束死胡同的例外。我做了一个发送和接收方法,但我的接收应该知道字符串的长度是由发送方方法发送的,我在不知道发送的字符串长度的情况下做了另一个同名的接收方法。
我的代码由 3 个类组成,如下所示:
package pipes1;
import java.io.*;
public class Pipe
{
private PipedWriter writer;
private PipedReader reader;
public PipedWriter getWriter()
{
return writer;
}
public PipedReader getReader()
{
return reader;
}
public Pipe()
{
writer = new PipedWriter();
reader = new PipedReader();
}
}
================================================ =========
package pipes1;
import java.io.IOException;
public class Person
{
private String name; //name of person
private String msg1;
private String msg2;
private Pipe pipe;
public String getMsg1()
{
return msg1;
}
public String getMsg2()
{
return msg2;
}
public Pipe getPipe()
{
return pipe;
}
public String getName()
{
return name;
}
public Person(String name,Pipe pipe,String s1,String s2)
{
this.name = name;
this.msg1 = s1;
this.msg2 = s2;
this.pipe = pipe;
}
public void connection(Person x) throws Throwable
{
pipe.getReader().connect(x.pipe.getWriter());
}
public void closing() throws IOException
{
this.pipe.getReader().close();
this.pipe.getWriter().close();
}
public void send(String m) throws IOException
{
this.pipe.getWriter().write(m);
this.pipe.getWriter().flush();
}
public void recieve() throws IOException
{
int data = this.pipe.getReader().read();
while(data!=-1)
{
System.out.print((char)data);
data = this.pipe.getReader().read();
}
System.out.println("");
}
public void recieve(String m) throws IOException
{
int i = 0;
while(i<m.length())
{
System.out.print((char) this.pipe.getReader().read());
i++;
}
System.out.println("");
}
}
================================================ ====================
package pipes1;
public class Main
{
public static void main(String[] args) throws Throwable
{
Pipe p1 = new Pipe();
Pipe p2 = new Pipe();
Person alice = new Person("Alice",p1,"recieved,thanks","hi bob");
Person bob = new Person("Bob",p2,"hi alice","recieved, thanks");
alice.connection(bob);
bob.connection(alice);
Thread terminal1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
bob.send(bob.getName()+":"+bob.getMsg1());
bob.recieve(alice.getName()+":"+alice.getMsg1());
bob.recieve(alice.getName()+":"+alice.getMsg2());
bob.send(bob.getName()+":"+bob.getMsg2());
bob.send("hi");
bob.send("hi");
}
catch (Throwable e)
{
System.out.println(e.getMessage());
}
}
});
//terminal of a
Thread terminal2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
alice.recieve(bob.getName()+":"+bob.getMsg1());
alice.send(alice.getName()+":"+alice.getMsg1());
alice.send(alice.getName()+":"+alice.getMsg2());
alice.recieve(bob.getName()+":"+bob.getMsg2());
alice.recieve();
alice.recieve();
}
catch (Throwable e)
{
System.out.println(e.getMessage());
}
}
});
terminal1.start();
terminal2.start();
}
}
================================================ ===================
结果是这样的:
鲍勃:嗨,爱丽丝
爱丽丝:收到,谢谢
爱丽丝:嗨,鲍勃
鲍勃:收到,谢谢
hihiWrite end dead
【问题讨论】:
-
使用
e.printStackTrace();而不是e.getMessage();来显示异常的完整堆栈跟踪。
标签: java multithreading pipe