From 8bb915ce5c8f8c1c5d687c0d0df8e49250cd670a Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 23 Apr 2025 23:17:29 +0200 Subject: [PATCH] u06-5 --- u06-5/Concurrent.java | 26 ++++++++++++++++++++++++++ u06-5/SpinLock.java | 20 ++++++++++++++++++++ u06-5/SpinLockTest.java | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 u06-5/Concurrent.java create mode 100644 u06-5/SpinLock.java create mode 100644 u06-5/SpinLockTest.java diff --git a/u06-5/Concurrent.java b/u06-5/Concurrent.java new file mode 100644 index 0000000..5c35d05 --- /dev/null +++ b/u06-5/Concurrent.java @@ -0,0 +1,26 @@ +abstract class Concurrent { + private ArgType[] args; + + public Concurrent(ArgType[] args) { + this.args = args; + } + + void run() { + Thread[] threads = new Thread[args.length]; + + for (int i = 0; i < args.length; i++) { + final ArgType arg = args[i]; + threads[i] = new Thread(() -> perform(arg)); + threads[i].start(); + } + for (int i = 0; i < threads.length; i++) { + try { + threads[i].join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + abstract void perform(ArgType arg); +} \ No newline at end of file diff --git a/u06-5/SpinLock.java b/u06-5/SpinLock.java new file mode 100644 index 0000000..d0014ef --- /dev/null +++ b/u06-5/SpinLock.java @@ -0,0 +1,20 @@ +import java.util.concurrent.atomic.AtomicReference; + +class SpinLock { + private AtomicReference currentThread; + + public SpinLock() { + currentThread = new AtomicReference(null); + } + + public void lock() { + Thread thisThread = Thread.currentThread(); + + // wait for currentThread to be null to acquire lock + while (!currentThread.compareAndSet(null, thisThread)); + } + + public boolean unlock() { + return currentThread.compareAndSet(Thread.currentThread(), null); + } +} \ No newline at end of file diff --git a/u06-5/SpinLockTest.java b/u06-5/SpinLockTest.java new file mode 100644 index 0000000..dd907fb --- /dev/null +++ b/u06-5/SpinLockTest.java @@ -0,0 +1,36 @@ +class SpinLockTest extends Concurrent { + + SpinLockTest(SpinLock[] args) { + super(args); + } + protected void perform(SpinLock lock) { + System.out.println(Thread.currentThread().getName() + + " wartet auf das Lock..."); + lock.lock(); + + System.out.println(Thread.currentThread().getName() + + " hat das Lock und behält es für etwa 1 Sek...."); + try { + Thread.sleep((long) (500 + Math.random()*1000)); + } catch (InterruptedException e) { + // Beim Schlafen zufaelliger Dauer unterbrochen zu werden ist kein Schaden + } + + if (lock.unlock()) { + System.out.println(Thread.currentThread().getName() + + " hat das von ihm vorher gesperrte Lock freigegeben und endet."); + } else { + System.out.println(Thread.currentThread().getName() + + " hat das Lock schon freigegeben vorgefunden!?"); + } + + } + + public static void main(String[] args) { + SpinLock sl = new SpinLock(); + SpinLock[] locks = {sl, sl, sl, sl, sl}; + // alle Threads benutzen dasselbe SpinLock sl + new SpinLockTest(locks).run(); + } +} +