为什么接收者线程在我的Java管道流媒体程序中没有收到任何内容?

时间:2017-04-26 16:12:32

标签: java multithreading synchronization message-passing

我有3个非常小的课程。

主要班级:

import java.io.*;

public class ConnectionManager {
    public static void main(String argv[]) {

        try {
            PipedOutputStream pout = new PipedOutputStream();
            PipedInputStream pin = new PipedInputStream(pout);

            Sender s = new Sender(pout, true);
            Receiver r = new Receiver(pin, true);
            System.out.println("Starting threads");
            s.start();
            r.start();
        } catch (Exception e) {}
    }
}

发件人类

import java.io.*;
import java.util.Random;

public class Sender extends Thread {
    ObjectOutputStream oos;
    boolean primitive;

    public Sender(OutputStream os, boolean primitive) {
        try {
            oos = new ObjectOutputStream(os);
        } catch (Exception e) {}
        this.primitive = primitive;
    }

    public void run() {
        Random rand = new Random();
        while (true) {
            try {
                System.out.println("Integer is being sent");
                oos.writeInt(10);
                Thread.sleep(1000);
            } catch (Exception e) {}
        }
    }
}

接收器类

import java.io.*;

public class Receiver extends Thread {
    ObjectInputStream ois;
    boolean primitive;

    public Receiver(InputStream is, boolean primitive) {
        try {
            ois = new ObjectInputStream(is);
        } catch (Exception e) {}
        this.primitive = primitive;
    }

    public void run() {
        System.out.println("Receiver is starting");
        while (true) {
            try {
                int x = ois.readInt();
                System.out.print("An int was read: " + x);
            } catch (Exception e) {}
        }
    }
}

请忽略看似未使用的变量,例如primitiverand。它们来自我之前测试过的略有不同的版本,我懒得去除它们。

无论如何,当我在ConnectionManager中运行main方法时,我将其作为输出:

Starting threads
Receiver is starting
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
//... ad infinitum 

为什么接收者线程没有收到通过管道传递的消息?我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

在您的代码中,存在java.io.IOException: Read end dead之类的异常。

您无法发现,因为您使用catch来抑制它们。

重点是您需要更改SenderReceiver类以使用PipedOutputStreamPipedInputStream,如下所示:

发件人类:

public class Sender extends Thread {
    PipedOutputStream oos;

    public Sender(PipedOutputStream os) {
        try {
            this.oos = os;
        } catch (Exception e) {System.out.println(e);}
    }

    public void run() {
        try {
                System.out.println("Integer is being sent");
                oos.write(10);
                oos.close();
                Thread.sleep(1000);
            } catch (Exception e) {System.out.println(e);}
        }
}

接收者类:

public class Receiver extends Thread {
    PipedInputStream ois;
    public Receiver(PipedInputStream is) {
        try {
            this.ois = is;
        } catch (Exception e) {System.out.println(e);}
    }

    public void run() {
        System.out.println("Receiver is starting");
          try {
                int x = ois.read();
                System.out.print("An int was read: " + x);
            } catch (Exception e) {System.out.println(e);}
    }
}

main()方法:

public static void main(String[] args) throws Exception {
         try {
                PipedOutputStream pout = new PipedOutputStream();
                PipedInputStream pin = new PipedInputStream(pout);

                Sender s = new Sender(pout);
                Receiver r = new Receiver(pin);
                System.out.println("Starting threads");
                s.start();
                r.start();
            } catch (Exception e) {
                System.out.println(e);
            }
     }

<强>输出:

Starting threads
Receiver is starting
Integer is being sent
An int was read: 10

作为旁注,请记住,catch是非常糟糕的做法,因为他们隐藏例外,所以我强烈建议不要使用它们在代码中。