From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/Listing2214.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2214.java') 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 -- cgit v1.2.3