diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java new file mode 100644 index 0000000..f75fe58 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java @@ -0,0 +1,74 @@ +/* Listing2214.java */
+
+import java.io.*;
+
+class Producer2214
+extends Thread
+{
+ private PipedOutputStream pipe;
+
+ public Producer2214(PipedOutputStream pipe)
+ {
+ this.pipe = pipe;
+ }
+
+ public void run()
+ {
+ while (true) {
+ byte b = (byte)(Math.random() * 128);
+ try {
+ pipe.write(b);
+ System.out.println("Produzent erzeugte " + b);
+ } catch (IOException e) {
+ System.err.println(e.toString());
+ }
+ try {
+ Thread.sleep((int)(100*Math.random()));
+ } catch (InterruptedException e) {
+ //nichts
+ }
+ }
+ }
+}
+
+class Consumer2214
+extends Thread
+{
+ private PipedInputStream pipe;
+
+ public Consumer2214(PipedInputStream pipe)
+ {
+ this.pipe = pipe;
+ }
+
+ public void run()
+ {
+ while (true) {
+ try {
+ byte b = (byte)pipe.read();
+ System.out.println(" Konsument fand " + b);
+ } catch (IOException e) {
+ System.err.println(e.toString());
+ }
+ try {
+ Thread.sleep((int)(100*Math.random()));
+ } catch (InterruptedException e) {
+ //nichts
+ }
+ }
+ }
+}
+
+public class Listing2214
+{
+ public static void main(String[] args)
+ throws Exception
+ {
+ PipedInputStream inPipe = new PipedInputStream();
+ PipedOutputStream outPipe = new PipedOutputStream(inPipe);
+ Producer2214 p = new Producer2214(outPipe);
+ Consumer2214 c = new Consumer2214(inPipe);
+ p.start();
+ c.start();
+ }
+}
\ No newline at end of file |
