gpt4 book ai didi

Java管道,为什么我在从管道读取时被无限期地阻塞

转载 作者:行者123 更新时间:2023-11-30 07:49:09 25 4
gpt4 key购买 nike

我正在尝试使用在2个线程之间通信的管道创建一个程序(你可以说2个线程之间的聊天),我的问题是当你编写时没有问题,但是当我读取通过管道传来的消息时我得到一个无限 block ,我找不到合适的解决方案,因此所有发送和接收的消息都显示在控制台中。所以我尝试从线程 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"

最佳答案

从管道读取数据被阻塞,因为它尚未关闭。这可能会起作用:

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

关于Java管道,为什么我在从管道读取时被无限期地阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com