Java管道,为什么我得到写结束死的异常

时间:2015-11-03 18:58:34

标签: java multithreading pipe

我试图使用在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

1 个答案:

答案 0 :(得分:1)

写入管道的线程在没有关闭管道的情况下结束,留下管道broken。随后尝试从PipedReader读取此内容并尝试IOException

来自方法PipedReader.read()的javadoc:

  

public int read()

     

抛出IOException

     

...

     

<强>抛出:

     

IOException - 如果管道损坏,未连接,关闭或发生I / O错误。

来自PipedInputStream的javadoc:

  

如果为连接的管道输出流提供数据字节的线程不再存在,则称管道已损坏。

我认为您可以通过在第一个帖子中添加bob.closing()来避免错误。 (我没有测试过。)每个编写器线程都应该关闭它正在编写的管道。