Skip to content

Commit b581782

Browse files
committed
feat: implement PipedInputStream and PipedOutputStream with Producer-Consumer example
WHAT: - Added PipedStreamExample demonstrating inter-thread communication using piped streams. - Implemented Producer thread that writes integer data sequentially to a PipedOutputStream. - Implemented Consumer thread that reads data from the connected PipedInputStream. - Established continuous data exchange between Producer and Consumer with a delay. WHY: - Piped streams in Java (`PipedInputStream` and `PipedOutputStream`) are designed for thread-to-thread communication. - This example models a classic producer-consumer problem using built-in stream constructs. - Demonstrates synchronization of data flow without explicit shared objects. HOW: 1. Created Producer class that writes incrementing integers to the output stream. 2. Created Consumer class that reads integers from the input stream and prints them. 3. Connected `PipedOutputStream` to `PipedInputStream` using `pis.connect(pos)`. 4. Started both threads to run concurrently with controlled sleep delays. BENEFITS: - Provides a clean mechanism for one thread to send data and another to receive it. - Avoids explicit buffer management; streams handle data flow internally. - Useful for pipeline-style processing where data is generated and consumed in parallel. REAL-WORLD APPLICATIONS: - Logging systems where one thread writes logs and another persists or processes them. - Streaming applications where producers generate continuous data (e.g., sensors, real-time feeds). - Threaded parsers or compilers that break work into producer-consumer stages. NOTES: - Both Producer and Consumer run indefinitely; in production systems, proper termination and resource cleanup should be added. - Each `os.write(count)` writes only the low-order byte; for multi-byte values, DataOutputStream/DataInputStream should be used. - Piped streams can throw `IOException` if either side closes unexpectedly. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 01d0c52 commit b581782

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

Section23JavaIOStreams/src/PipedStreamExample.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import java.io.PipedInputStream;
44
import java.io.PipedOutputStream;
55

6-
// Producer class writes data to OutputStream
6+
// Producer class writes data to OutputStream.
77
class Producer extends Thread {
88
OutputStream os;
99

10-
// Constructor to initialize OutputStream
10+
// Constructor to initialize OutputStream.
1111
public Producer(OutputStream o) {
1212
this.os = o;
1313
}
@@ -16,10 +16,12 @@ public void run() {
1616
int count = 1;
1717
try {
1818
while (true) {
19-
os.write(count); // Writing data to output stream
20-
os.flush(); // Ensuring data is sent out
19+
os.write(count); // Writing data to the output stream.
20+
os.flush(); // Ensuring data is sent out.
21+
2122
System.out.println("Producer: " + count);
2223
System.out.flush();
24+
2325
count++;
2426
Thread.sleep(500);
2527
}
@@ -29,11 +31,11 @@ public void run() {
2931
}
3032
}
3133

32-
// Consumer class reads data from InputStream
34+
// Consumer class reads data from InputStream.
3335
class Consumer extends Thread {
3436
InputStream is;
3537

36-
// Constructor to initialize InputStream
38+
// Constructor to initialize InputStream.
3739
public Consumer(InputStream s) {
3840
this.is = s;
3941
}
@@ -55,18 +57,56 @@ public void run() {
5557

5658
public class PipedStreamExample {
5759
public static void main(String[] args) throws Exception {
58-
// Creating Piped Input and Output Streams
60+
// Creating Piped Input and Output Streams.
5961
PipedInputStream pis = new PipedInputStream();
6062
PipedOutputStream pos = new PipedOutputStream();
6163

62-
// Connecting PipedInputStream to PipedOutputStream
64+
// Connecting PipedInputStream to PipedOutputStream.
6365
pis.connect(pos);
6466

6567
Producer p = new Producer(pos);
6668
Consumer c = new Consumer(pis);
6769

68-
// Starting Threads
6970
p.start();
7071
c.start();
7172
}
72-
}
73+
}
74+
75+
/*
76+
1. Piped Streams:
77+
- `PipedOutputStream` (Producer likhta hai) aur `PipedInputStream`
78+
(Consumer padhta hai) ek doosre ke saath connect kiye jaate hain.
79+
- Ek thread data write karta hai (Producer), dusra thread same data read karta hai (Consumer).
80+
- Ye thread-to-thread communication ka ek tareeka hai Java IO me.
81+
82+
2. Producer:
83+
- Producer continuously numbers (1, 2, 3...) likhta hai `os.write(count)`.
84+
- `flush()` ensure karta hai ki buffer me padha hua data consumer ko turant bhej diya jaye.
85+
- `Thread.sleep(500)` → thoda delay deta hai taaki output readable ho.
86+
87+
3. Consumer:
88+
- Consumer continuously `is.read()` se Producer ka bheja hua data read karta hai.
89+
- Jo bhi byte Producer bhejta hai, wahi yaha receive hota hai.
90+
- `Thread.sleep(500)` → thoda delay, sync output ke liye.
91+
92+
4. Main Method:
93+
- `PipedInputStream pis = new PipedInputStream();`
94+
- `PipedOutputStream pos = new PipedOutputStream();`
95+
- Dono ko connect kiya gaya: `pis.connect(pos);`
96+
- Producer ko `pos` diya gaya (write ke liye).
97+
- Consumer ko `pis` diya gaya (read ke liye).
98+
- Dono threads start hote hi ek pipeline jaise kaam karta hai.
99+
100+
5. Output Pattern:
101+
Producer: 1
102+
Consumer: 1
103+
Producer: 2
104+
Consumer: 2
105+
... aur aise hi chalti rahegi.
106+
107+
✔ `PipedOutputStream` → Producer likhta hai.
108+
✔ `PipedInputStream` → Consumer padhta hai.
109+
✔ Dono ko connect karke inter-thread communication hota hai.
110+
✔ Achha example Producer-Consumer problem ka using IO Streams.
111+
✔ Real-world use case: background threads jo ek doosre ko data stream karte hain.
112+
*/

0 commit comments

Comments
 (0)