From 8577edd5e7d435d3121af48b21867c2d2529f94a Mon Sep 17 00:00:00 2001 From: Selebrator Date: Thu, 13 Jun 2019 23:49:57 +0200 Subject: [PATCH] =?UTF-8?q?Ein=20paar=20alternative=20L=C3=B6sungen=20f?= =?UTF-8?q?=C3=BCr=2010.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/_10/_4/lukas/NameOutput.java | 14 +++++++ .../_10/_4/lukas/NameOutputBusyWaiting.java | 31 ++++++++++++++++ .../java/_10/_4/lukas/NameOutputFairLock.java | 22 +++++++++++ .../_10/_4/lukas/NameOutputWaitNotify.java | 37 +++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/_10/_4/lukas/NameOutput.java create mode 100644 src/main/java/_10/_4/lukas/NameOutputBusyWaiting.java create mode 100644 src/main/java/_10/_4/lukas/NameOutputFairLock.java create mode 100644 src/main/java/_10/_4/lukas/NameOutputWaitNotify.java diff --git a/src/main/java/_10/_4/lukas/NameOutput.java b/src/main/java/_10/_4/lukas/NameOutput.java new file mode 100644 index 0000000..0d56e87 --- /dev/null +++ b/src/main/java/_10/_4/lukas/NameOutput.java @@ -0,0 +1,14 @@ +package _10._4.lukas; + +public abstract class NameOutput { + + private NameOutput() { + } + + public static void main(String[] args) { + final int n = 3; + for(int i = 0; i < n; i++) { + new NameOutputFairLock().start(); + } + } +} diff --git a/src/main/java/_10/_4/lukas/NameOutputBusyWaiting.java b/src/main/java/_10/_4/lukas/NameOutputBusyWaiting.java new file mode 100644 index 0000000..eead11f --- /dev/null +++ b/src/main/java/_10/_4/lukas/NameOutputBusyWaiting.java @@ -0,0 +1,31 @@ +package _10._4.lukas; + +// ordered by creation time, deadlocks if any thread in the chain is interrupted. +public class NameOutputBusyWaiting extends Thread { + + private static final Object LOCK = new Object(); + + private static int counter = 0; + private static int total = 0; + + private final int number; + + public NameOutputBusyWaiting() { + synchronized(LOCK) { + this.number = total; + total++; + } + } + + @Override + public void run() { + while(!this.isInterrupted()) { + synchronized(LOCK) { + if(this.number == counter) { + System.out.println(this.getName()); + counter = (counter + 1) % total; + } + } + } + } +} diff --git a/src/main/java/_10/_4/lukas/NameOutputFairLock.java b/src/main/java/_10/_4/lukas/NameOutputFairLock.java new file mode 100644 index 0000000..d7e6ccf --- /dev/null +++ b/src/main/java/_10/_4/lukas/NameOutputFairLock.java @@ -0,0 +1,22 @@ +package _10._4.lukas; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +// ordered by least recent access since creation +public class NameOutputFairLock extends Thread { + + private static final Lock LOCK = new ReentrantLock(true); + + @Override + public void run() { + while(!this.isInterrupted()) { + LOCK.lock(); + try { + System.out.println(this.getName()); + } finally { + LOCK.unlock(); + } + } + } +} diff --git a/src/main/java/_10/_4/lukas/NameOutputWaitNotify.java b/src/main/java/_10/_4/lukas/NameOutputWaitNotify.java new file mode 100644 index 0000000..bccc503 --- /dev/null +++ b/src/main/java/_10/_4/lukas/NameOutputWaitNotify.java @@ -0,0 +1,37 @@ +package _10._4.lukas; + +// ordered by creation time, deadlocks if any thread in the chain is interrupted. +public class NameOutputWaitNotify extends Thread { + + private static final Object LOCK = new Object(); + + private static int counter = 0; + private static int total = 0; + + private final int number; + + public NameOutputWaitNotify() { + synchronized(LOCK) { + this.number = total; + total++; + } + } + + @Override + public void run() { + while(!this.isInterrupted()) { + synchronized(LOCK) { + while(this.number != counter) { + try { + LOCK.wait(); + } catch(InterruptedException e) { + System.out.println(this.getName() + " was interrupted"); + } + } + System.out.println(this.getName()); + counter = (counter + 1) % total; + LOCK.notifyAll(); + } + } + } +}