Commit b581782
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
1 file changed
+50
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | | - | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | | - | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
62 | | - | |
| 64 | + | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
68 | | - | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
0 commit comments