为什么接收器线程在我的Java管道流式程序中没有接收到任何内容? [英] Why is the receiver thread not receiving anything in my Java piped streaming program?

查看:102
本文介绍了为什么接收器线程在我的Java管道流式程序中没有接收到任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个非常小的班级.

I have 3 very small classes.

主要类别:

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.它们是我之前测试的版本稍有不同的保留,我懒得删除它们.

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.

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

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?

推荐答案

在您的代码中,有一个类似java.io.IOException: Read end dead的异常.

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

您无法发现,因为您要用空的catch方块来压制它们.

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

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

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

发件人类别:

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是非常不好的做法,因为它们隐藏异常,因此,我强烈建议不要在代码.


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.

这篇关于为什么接收器线程在我的Java管道流式程序中没有接收到任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆