Commit c96582a
committed
docs(io): add detailed explanation of Piped Streams for inter-thread communication
What
- Documented concept of **Piped Streams in Java** (PipedInputStream + PipedOutputStream).
- Covered:
- Definition and purpose (inter-thread data communication).
- Core components and their roles.
- Step-by-step working mechanism (producer → output stream → internal buffer → input stream → consumer).
- Blocking behavior when buffer is empty/full.
- Stream connection requirement using `.connect()`.
- Listed **advantages** (efficiency, no shared memory, producer-consumer handling).
- Highlighted **disadvantages** (blocking, exception handling, limited buffer).
Why
- Piped Streams are a foundational Java I/O mechanism for **producer-consumer problems** and inter-thread communication.
- Unlike shared memory approaches, they allow **direct streaming of data between threads**.
- Understanding their blocking nature and buffer limitations is crucial for writing safe concurrent programs.
Logic
1. **Producer writes to PipedOutputStream**
- Data is stored in the pipe’s internal buffer.
- If buffer is full → producer thread blocks until space is available.
2. **Consumer reads from PipedInputStream**
- Retrieves bytes from the internal buffer.
- If buffer is empty → consumer thread blocks until producer writes data.
3. **Connection**
- Must explicitly connect `PipedInputStream` with `PipedOutputStream` via `.connect()` before usage.
- Once connected, data flows seamlessly across threads.
4. **Thread Synchronization**
- The blocking nature implicitly synchronizes producer and consumer threads.
- Prevents data corruption without needing explicit locks.
Real-world applications
- **Producer-Consumer problems**: logging systems, event pipelines, data streaming tasks.
- **Multithreaded processing**: one thread generates data (producer), another processes it (consumer).
- **Simulation of Unix-like pipes** in Java programs.
- **Messaging systems** within applications where lightweight communication is needed without shared variables.
Notes
- Buffer size is fixed (default 1024 bytes unless specified).
- Exceptions (e.g., IOException, Pipe broken) must be handled to avoid thread crashes.
- Avoid connecting multiple input/output streams to the same pipe — unsupported and error-prone.
- Works only for **intra-process thread communication** (not across JVMs or machines).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3e49347 commit c96582a
File tree
2 files changed
+106
-83
lines changed- JAVA8/StreamsInJAVA/PrimitiveStreams/src
- Java 8 Crash Course/Java 8 Streams/Primitive Streams/src
2 files changed
+106
-83
lines changedLines changed: 0 additions & 83 deletions
This file was deleted.
Java 8 Crash Course/Java 8 Streams/Primitive Streams/src/Primitive streams various application.txt
Lines changed: 106 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 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 | + | |
0 commit comments