我正在尝试使用在2个线程之间进行通信的管道创建一个程序(你可以说两个线程之间的聊天),我的问题在于你写的时候没有问题,但是当我通过管道读取消息时得到一个无限的块,我找不到合适的解决方案,所以发送的所有消息&收到的信息显示在控制台中所以我试图从线程1发送消息到线程2并且它工作,但是从线程2到1,有一个块。
我的程序由3个类组成,我将在下面显示:
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");
Thread terminal1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
bob.connection(alice);
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("1 to 2\n");
bob.recieve();
bob.recieve();
bob.send("ack 1\n");
bob.closing();
}
catch (Throwable e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
});
//terminal of a
Thread terminal2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
alice.connection(bob);
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.send("2 to 1\n");
alice.send("ack 2\n");
alice.recieve();
alice.closing();
}
catch (Throwable e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
});
terminal1.start();
terminal2.start();
}
}
=============================================== ==========================
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;
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();
}
}
=============================================== ======
并且控制台的结果总是像这样
Bob:hi alice
Alice:recieved, thanks
Alice:hi bob
Bob:recieved, thanks
1 to 2
// here comes the block , thread 1 can't get the message "2 to 1"
答案 0 :(得分:1)
从管道读取是阻塞的,因为它尚未关闭。这可能会奏效:
public void receive() throws IOException {
while(this.pipe.getReader().ready()) {
int data = this.pipe.getReader().read();
System.out.print((char) data);
}
System.out.println();
}
更新:我错了,这就足够了。比较.read() == -1
仅用于测试管道的另一侧是否关闭,以及管道上的最后一个字符是否已收到。测试.ready()
是当时管道上是否有任何字符。因为发送和接收是在两个线程上,准备就绪是不确定的,如果你没准备就停止阅读,你可能会得到一个部分线。
考虑到这一点,您需要一个不同的测试。我会用终结符令牌(例如0)附加每条消息,然后在另一端附加。所以这是我已经测试过的解决方案:
public void send(String m) throws IOException {
this.pipe.getWriter().write(m);
this.pipe.getWriter().write(0);
this.pipe.getWriter().flush();
}
public void receive() throws IOException {
while(true) {
int data = this.pipe.getReader().read();
if (data == 0) break;
System.out.print((char) data);
}
System.out.println();
}
public void receive(String m) throws IOException {
for (int i = 0; i < m.length(); i++) {
System.out.print((char) this.pipe.getReader().read());
}
this.pipe.getReader().read(); // consume the terminator
System.out.println();
}