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/Listing2213.java | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing2213.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2213.java') diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing2213.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2213.java new file mode 100644 index 0000000..ebd0227 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2213.java @@ -0,0 +1,82 @@ +/* Listing2213.java */ + +import java.util.*; + +class Producer2213 +extends Thread +{ + private Vector v; + + public Producer2213(Vector v) + { + this.v = v; + } + + public void run() + { + String s; + + while (true) { + synchronized (v) { + s = "Wert "+Math.random(); + v.addElement(s); + System.out.println("Produzent erzeugte "+s); + v.notify(); + } + try { + Thread.sleep((int)(100*Math.random())); + } catch (InterruptedException e) { + //nichts + } + } + } +} + +class Consumer2213 +extends Thread +{ + private Vector v; + + public Consumer2213(Vector v) + { + this.v = v; + } + + public void run() + { + while (true) { + synchronized (v) { + if (v.size() < 1) { + try { + v.wait(); + } catch (InterruptedException e) { + //nichts + } + } + System.out.print( + " Konsument fand "+(String)v.elementAt(0) + ); + v.removeElementAt(0); + System.out.println(" (verbleiben: "+v.size()+")"); + } + try { + Thread.sleep((int)(100*Math.random())); + } catch (InterruptedException e) { + //nichts + } + } + } +} + +public class Listing2213 +{ + public static void main(String[] args) + { + Vector v = new Vector(); + + Producer2213 p = new Producer2213(v); + Consumer2213 c = new Consumer2213(v); + p.start(); + c.start(); + } +} \ No newline at end of file -- cgit v1.2.3