Inter thread communication in java (Example/ PipedInputStream /PipedOutputStream)

  • Inter thread communication is very common phenomenon.
  • There are many ways to achieve inter thread communication.
  • In current post, we will use pipes to achieve thread communication.
  • We will create couple of threaders namely producer & consumer.
    • Producer thread will produce the data & consumer thread will consume the data.

1. PipedOutputStream class:

  • A piped output stream can be connected to a piped input stream to create a communications pipe.
  • The piped output stream is the sending end of the pipe.
  • Typically, data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread.

PipedOutputStream has following constructors:

S.No Constructor Description
1 PipedOutputStream() Creates a piped output stream that is not yet connected to a piped input stream.
2 PipedOutputStream(PipedInputStream snk) Creates a piped output stream connected to the specified piped input stream.

2. PipedInputStream class:

  • A piped input stream should be connected to a piped output stream.
    • the piped input stream then provides whatever data bytes are written to the piped output stream.
  • Typically, data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread.

PipedInputStream has following constructors:

S.No Constructor Description
1 PipedInputStream() Creates a PipedInputStream so that it is not yet connected.
2 PipedInputStream(int pipeSize) Creates a PipedInputStream so that it is not yet connected and uses the specified pipe size for the pipe's buffer.
3 PipedInputStream(PipedOutputStream src) Creates a PipedInputStream so that it is connected to the piped output stream src.
4 PipedInputStream(PipedOutputStream src, int pipeSize) Creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe's buffer.

3. Producer consumer in java (PipedOutputStream/PipedInputStream)

  1. Create producer & consumer threads.
  2. Create PipedOutputStream & PipedInputStream objects.
    • Connect PipedOutputStream to PipedInputStream.
  3. Producer thread writes data to PipedOutputStream.
  4. Consumer thread will read data from PipedInputStream.
producer consumer pipes thread communication
Fig 1: Producer consumer thread communications

4. Producer consumer thread communication in java using pipes (example)

package org.learn.concurrency;

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

public class InterThreadPipeIO {
    public static void main(String[] args) throws IOException, InterruptedException {
        PipedOutputStream pipedOutputStream = new PipedOutputStream();
        PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);

        Producer producer = new Producer(pipedOutputStream);
        Consumer consumer = new Consumer(pipedInputStream);

        Thread pThread = new Thread(producer);
        Thread cThread = new Thread(consumer);

        pThread.start();
        cThread.start();

        pThread.join();
        cThread.join();
        pipedOutputStream.close();
        pipedInputStream.close();
    }
}

class Producer implements Runnable {

    private final PipedOutputStream pipedOutputStream;

    public Producer(PipedOutputStream pipedOutputStream) {
        this.pipedOutputStream = pipedOutputStream;
    }

    @Override
    public void run() {

        int index = 0;
        try {
            while (index <= 25) {
                System.out.println("Producer thread generating: " + index);
                pipedOutputStream.write(index);
                Thread.sleep(50);
                index++;
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                pipedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer implements Runnable {

    private final PipedInputStream pipedInputStream;

    public Consumer(PipedInputStream pipedInputStream) {
        this.pipedInputStream = pipedInputStream;
    }

    @Override
    public void run() {
        try {
            while (true) {
                int value = pipedInputStream.read();
                System.out.println("Consumer thread consuming: " + value);
                Thread.sleep(50);
                if (value == 25)
                    break;
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                pipedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

5. Producer consumer thread communication in java using pipes (example)

Producer thread generating: 0
Producer thread generating: 1
Producer thread generating: 2
Producer thread generating: 3
Producer thread generating: 4
Producer thread generating: 5
Producer thread generating: 6
Producer thread generating: 7
Producer thread generating: 8
Producer thread generating: 9
Producer thread generating: 10
Producer thread generating: 11
Producer thread generating: 12
Producer thread generating: 13
Producer thread generating: 14
Producer thread generating: 15
Producer thread generating: 16
Producer thread generating: 17
Producer thread generating: 18
Producer thread generating: 19
Consumer thread consuming: 0
Producer thread generating: 20
Consumer thread consuming: 1
Producer thread generating: 21
Consumer thread consuming: 2
Producer thread generating: 22
Consumer thread consuming: 3
Producer thread generating: 23
Consumer thread consuming: 4
Producer thread generating: 24
Consumer thread consuming: 5
Producer thread generating: 25
Consumer thread consuming: 6
Consumer thread consuming: 7
Consumer thread consuming: 8
Consumer thread consuming: 9
Consumer thread consuming: 10
Consumer thread consuming: 11
Consumer thread consuming: 12
Consumer thread consuming: 13
Consumer thread consuming: 14
Consumer thread consuming: 15
Consumer thread consuming: 16
Consumer thread consuming: 17
Consumer thread consuming: 18
Consumer thread consuming: 19
Consumer thread consuming: 20
Consumer thread consuming: 21
Consumer thread consuming: 22
Consumer thread consuming: 23
Consumer thread consuming: 24
Consumer thread consuming: 25
Scroll to Top