From 2f4251ad535fed358b5dd5facc1890094273a563 Mon Sep 17 00:00:00 2001 From: Lisa Date: Tue, 18 Jun 2019 19:50:06 +0200 Subject: [PATCH] Initial commit 11.1 and 11.2 --- src/main/java/_11/_1/InOut.java | 46 +++++++++++++ src/main/java/_11/_2/Barriers.java | 101 +++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/_11/_1/InOut.java create mode 100644 src/main/java/_11/_2/Barriers.java diff --git a/src/main/java/_11/_1/InOut.java b/src/main/java/_11/_1/InOut.java new file mode 100644 index 0000000..c82bcc5 --- /dev/null +++ b/src/main/java/_11/_1/InOut.java @@ -0,0 +1,46 @@ +package _11._1; + +import provided.IO; +import java.util.concurrent.Semaphore; + +class Output extends Thread { + + public void run() { + try { + InOut.getSemaphore().acquire(); + System.out.println(InOut.getValue() * InOut.getValue()); + } catch (InterruptedException e) { + + } + } +} + +class Input extends Thread { + + public void run() { + InOut.setValue(IO.readInt("Value: ")); + InOut.getSemaphore().release(); + } +} + +public class InOut { + + private static Semaphore semaphore = new Semaphore(0); + private static int value = 0; + + public static Semaphore getSemaphore() { return semaphore; } + + public static int getValue() { + return value; + } + + public static void setValue(int value) { + InOut.value = value; + } + + public static void main(String[] args) { + new Output().start(); + new Input().start(); + } + +} diff --git a/src/main/java/_11/_2/Barriers.java b/src/main/java/_11/_2/Barriers.java new file mode 100644 index 0000000..6902724 --- /dev/null +++ b/src/main/java/_11/_2/Barriers.java @@ -0,0 +1,101 @@ +package _11._2; + +public class Barriers { + + private final static int NUMBER = 3; + + public static void main(String[] args) { + Counter obj = new Counter(NUMBER); + NumberRunner[] runner = new NumberRunner[NUMBER]; + for (int i = 0; i < NUMBER; i++) { + runner[i] = new NumberRunner(i, obj); + } + for (int i = 0; i < NUMBER; i++) { + runner[i].start(); + } + } + +} + +class Counter { + private final int numb; // Anzahl Threads + private int numberIn = 0; // Anzahl der Threads an Sperre + private int numberOut; // Anzahl der laufenden Threads + + public Counter(int number) { + this.numb = number; + this.numberOut = number; + } + + public int getMax() { return this.numb; } + + public int getIn() { + return this.numberIn; + } + + public int getOut() { return this.numberOut; } + + public void resetIn() { this.numberIn = 0; } + + public void resetOut() { this.numberOut = 0; } + + public void incrementIn() { this.numberIn++; } + + public void incrementOut() { this.numberOut++; } + +} + +class NumberRunner extends Thread { + + private int number; + private final Counter obj; + + public NumberRunner(int n, Counter obj) { + this.number = n; + this.obj = obj; + } + + @Override + public void run() { + for (int i = 0; i < 1000; i++) { + + System.out.println("Thread " + number + ": " + i); + + if (i%10 == 0 && i != 0) { + synchronized (obj) { + try { + // warte, falls andere Threads von vorheriger Sperre noch nicht wieder gestartet sind + while (obj.getOut() < obj.getMax()) { + obj.wait(); + } + + // Thread kommt an aktueller Sperre an + obj.incrementIn(); + + // letzter Thread, der ankommt, benachrichtigt alle, dass es weiter gehen kann + if (obj.getIn() >= obj.getMax()) { + obj.resetOut(); + obj.notifyAll(); + } + + // Threads warten, bis alle angekommen sind + while (obj.getIn() < obj.getMax()) { + obj.wait(); + } + + // Thread startet wieder + obj.incrementOut(); + + // der letzte Thread, der startet, benachrichtigt alle, dass die naechste Sperre freigegeben ist + if (obj.getOut() == obj.getMax()) { + obj.resetIn(); + obj.notifyAll(); + } + + } catch (InterruptedException e) { + } + } + } + } + } +}