PipedInputStream,PipedOutputStream实例,线程间通信

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/82898175

Java.io.PipedOutputStream and java.io.PipedInputStream has been introduced in JDK 1.0. PipedOutputStream and PipedInputStream both are connected to each other to create a communication pipe. PipedOutputStream is the sending end and PipedOutputStream is the receiving end of the pipe. Both ends should not be handled by single thread otherwise deadlock may occur. If any thread stops working, pipe is said to be broken.

Java PipedOutputStream and PipedInputStream Example

最好不要在一个线程中使用,可能会发生死锁。

如果有任一个线程停止工作或发生异常,管道就会被破坏,不能正常的发送或接收。

实例1:线程池使用


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PipedInputOutputDemo {
    final static PipedOutputStream pipedOut = new PipedOutputStream();//输出,发送端,写入流中
    final static PipedInputStream pipedIn = new PipedInputStream();//输入,接收端,从流中读取

    class PipedOutputThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i <= 5; i++) {
                    pipedOut.write(("Message " + i + "\n").getBytes());
                    Thread.sleep(500);
                }
                pipedOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class PipedInputThread implements Runnable {
        @Override
        public void run() {
            try {
                int i = 0;
                while ((i = pipedIn.read()) != -1) {
                    System.out.print((char) i);
                }
                pipedIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            pipedOut.connect(pipedIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(new PipedInputOutputDemo().new PipedOutputThread());
        service.execute(new PipedInputOutputDemo().new PipedInputThread());
    }
}

输出:

Message 1
Message 2
Message 3
Message 4
Message 5

实例2:两个单独的线程使用


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputOutputDemo {
    final static PipedOutputStream pipedOut = new PipedOutputStream();//输出,发送端,写入流中
    final static PipedInputStream pipedIn = new PipedInputStream();//输入,接收端,从流中读取

    class PipedOutputThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i <= 5; i++) {
                    pipedOut.write(("Message " + i + "\n").getBytes());
                    Thread.sleep(500);
                }
                pipedOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class PipedInputThread implements Runnable {
        @Override
        public void run() {
            try {
                int i = 0;
                while ((i = pipedIn.read()) != -1) {
                    System.out.print((char) i);
                }
                pipedIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void f1() {
        Thread pipedOutputThread = new Thread(new PipedOutputThread());
        Thread pipedInputThread = new Thread(new PipedInputThread());
        pipedOutputThread.start();
        pipedInputThread.start();
    }


    public static void main(String[] args) {
        try {
            pipedOut.connect(pipedIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        new PipedInputOutputDemo().f1();
    }
}

输出:

Message 1
Message 2
Message 3
Message 4
Message 5

参考:https://www.concretepage.com/forum/login

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/82898175