Java 当与两个线程通信时,我必须使用管道吗?

Java 当与两个线程通信时,我必须使用管道吗?,java,multithreading,Java,Multithreading,最近我深入研究了线的黑暗艺术,我了解了如何创建线,何时使用线,何时不使用线。但是当我试图学习如何在他们之间交流时;我发现管道是你用来做这件事的。我有一个对象,它是我创建的一个类的实例,但管道似乎只能发送字节数组或整数。我习惯于无法使用对象流之类的东西将我的对象发送到另一个线程,但我的互联网冲浪变得非常糟糕,我迷路了。所以我想唯一要做的就是转向堆栈溢出,看看是否有人能帮上忙。提前感谢您的帮助。您应该使用的实现之一 我通常使用它,因为它允许我限制解决方案的内存占用。A可以用于无限大小,但请确保不能过

最近我深入研究了线的黑暗艺术,我了解了如何创建线,何时使用线,何时不使用线。但是当我试图学习如何在他们之间交流时;我发现管道是你用来做这件事的。我有一个对象,它是我创建的一个类的实例,但管道似乎只能发送字节数组或整数。我习惯于无法使用对象流之类的东西将我的对象发送到另一个线程,但我的互联网冲浪变得非常糟糕,我迷路了。所以我想唯一要做的就是转向堆栈溢出,看看是否有人能帮上忙。提前感谢您的帮助。

您应该使用的实现之一

我通常使用它,因为它允许我限制解决方案的内存占用。A可以用于无限大小,但请确保不能过载内存

下面是两个线程使用
ArrayBlockingQueue
在它们之间进行通信

public class TwoThreads {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("TwoThreads:Test");
        new TwoThreads().test();
    }

    // The end of the list.
    private static final Integer End = -1;

    static class Producer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Producer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i < 1000; i++) {
                    queue.add(i);
                    Thread.sleep(1);
                }
                // Finish the queue.
                queue.add(End);
            } catch (InterruptedException ex) {
                // Just exit.
            }
        }

    }

    static class Consumer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Consumer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            boolean ended = false;
            while (!ended) {
                try {
                    Integer i = queue.take();
                    ended = i == End;
                    System.out.println(i);
                } catch (InterruptedException ex) {
                    ended = true;
                }
            }
        }

    }

    public void test() throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
        Thread pt = new Thread(new Producer(queue));
        Thread ct = new Thread(new Consumer(queue));
        // Start it all going.
        pt.start();
        ct.start();
        // Wait for it to finish.
        pt.join();
        ct.join();
    }

}
公共类双线程{
公共静态void main(字符串args[])引发InterruptedException{
System.out.println(“两个线程:测试”);
新的TwoThreads().test();
}
//列表的末尾。
私有静态最终整数End=-1;
静态类生成器实现Runnable{
最终阻塞队列;
公共生产者(封锁队列){
this.queue=队列;
}
@凌驾
公开募捐{
试一试{
对于(int i=0;i<1000;i++){
加入(i);
睡眠(1);
}
//排完队。
队列。添加(结束);
}捕获(中断异常例外){
//出去吧。
}
}
}
静态类使用者实现可运行{
最终阻塞队列;
公共消费者(封锁队列){
this.queue=队列;
}
@凌驾
公开募捐{
布尔结束=假;
当(!结束){
试一试{
整数i=queue.take();
结束=i==结束;
系统输出打印LN(i);
}捕获(中断异常例外){
结束=真;
}
}
}
}
public void test()引发InterruptedException{
BlockingQueue=新建LinkedBlockingQueue();
线程pt=新线程(新生产者(队列));
线程ct=新线程(新使用者(队列));
//开始一切吧。
pt.start();
ct.start();
//等它结束。
pt.join();
ct.join();
}
}

您应该使用的实现之一

我通常使用它,因为它允许我限制解决方案的内存占用。A可以用于无限大小,但请确保不能过载内存

下面是两个线程使用
ArrayBlockingQueue
在它们之间进行通信

public class TwoThreads {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("TwoThreads:Test");
        new TwoThreads().test();
    }

    // The end of the list.
    private static final Integer End = -1;

    static class Producer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Producer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i < 1000; i++) {
                    queue.add(i);
                    Thread.sleep(1);
                }
                // Finish the queue.
                queue.add(End);
            } catch (InterruptedException ex) {
                // Just exit.
            }
        }

    }

    static class Consumer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Consumer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            boolean ended = false;
            while (!ended) {
                try {
                    Integer i = queue.take();
                    ended = i == End;
                    System.out.println(i);
                } catch (InterruptedException ex) {
                    ended = true;
                }
            }
        }

    }

    public void test() throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
        Thread pt = new Thread(new Producer(queue));
        Thread ct = new Thread(new Consumer(queue));
        // Start it all going.
        pt.start();
        ct.start();
        // Wait for it to finish.
        pt.join();
        ct.join();
    }

}
公共类双线程{
公共静态void main(字符串args[])引发InterruptedException{
System.out.println(“两个线程:测试”);
新的TwoThreads().test();
}
//列表的末尾。
私有静态最终整数End=-1;
静态类生成器实现Runnable{
最终阻塞队列;
公共生产者(封锁队列){
this.queue=队列;
}
@凌驾
公开募捐{
试一试{
对于(int i=0;i<1000;i++){
加入(i);
睡眠(1);
}
//排完队。
队列。添加(结束);
}捕获(中断异常例外){
//出去吧。
}
}
}
静态类使用者实现可运行{
最终阻塞队列;
公共消费者(封锁队列){
this.queue=队列;
}
@凌驾
公开募捐{
布尔结束=假;
当(!结束){
试一试{
整数i=queue.take();
结束=i==结束;
系统输出打印LN(i);
}捕获(中断异常例外){
结束=真;
}
}
}
}
public void test()引发InterruptedException{
BlockingQueue=新建LinkedBlockingQueue();
线程pt=新线程(新生产者(队列));
线程ct=新线程(新使用者(队列));
//开始一切吧。
pt.start();
ct.start();
//等它结束。
pt.join();
ct.join();
}
}

查看其他选项有类似的问题,但我找不到一个有我的答案或问了我的问题。你从哪里获得管道?这似乎超出了左外野。您指的是不同的进程吗?pipoutputstreams和PipInputStreams用于使用字节[]与线程通信。您一定在考虑unix管道?查看其他选项有类似的问题,但我找不到一个有我的答案或我问的问题。您从哪里获得管道?这似乎超出了左外野。您指的是不同的进程吗?pipoutputstreams和PipInputStreams用于使用字节[]与线程通信。你一定在想unix管道?这正是我想要的。这个编程的东西,我只是不断学习!!!非常感谢。这正是我要找的。这个编程的东西,我只是不断学习!!!非常感谢。