Why is the receiver thread not receiving anything in my Java piped streaming program? [duplicate]

Deadly 提交于 2019-12-26 11:38:26

问题


I have 3 very small classes.

The main class:

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) {}
    }
}

The Sender class

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) {}
        }
    }
}

And the Receiver class

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) {}
        }
    }
}

Please ignore seemingly unused variables like primitive and rand. They're holdovers from slightly different versions that I was testing out earlier and I was too lazy to remove them.

Anyway, when I run the main method in ConnectionManager, I get this as output:

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 

Why is the receiver thread not getting the messages that are piped through? What am I missing here?


回答1:


In your code, there is an exception like java.io.IOException: Read end dead.

You are NOT able to spot because you are suppressing them with empty catch blocks.

The main point is that you need to change Sender and Receiver classes to use PipedOutputStream and PipedInputStream as shown below :

Sender class:

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

Receiver class:

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() method:

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

OUTPUT:

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

As a side note, remember that, empty catch blocks are very bad practice as they hide the exceptions, so I strongly suggest not to use them in the code.



来源:https://stackoverflow.com/questions/43639487/why-is-the-receiver-thread-not-receiving-anything-in-my-java-piped-streaming-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!