From 00b86012083c170700214da1536e7ee8657e31e9 Mon Sep 17 00:00:00 2001 From: Shahnam Javidnia <3015418@stud.hs-mannheim.de> Date: Tue, 20 May 2025 16:43:10 +0200 Subject: [PATCH] first test everything work fine (for now) --- .../src/main/java/pp/Main.java | 1 + .../java/pp/ThreadsafeSimplifiedList.java | 136 +++++++++--------- 2 files changed, 70 insertions(+), 67 deletions(-) diff --git a/pp.A4-HandOverHandLocking/src/main/java/pp/Main.java b/pp.A4-HandOverHandLocking/src/main/java/pp/Main.java index 48b71a1..26e7031 100644 --- a/pp.A4-HandOverHandLocking/src/main/java/pp/Main.java +++ b/pp.A4-HandOverHandLocking/src/main/java/pp/Main.java @@ -1,5 +1,6 @@ package pp; + public class Main { static SimplifiedList list; diff --git a/pp.A4-HandOverHandLocking/src/main/java/pp/ThreadsafeSimplifiedList.java b/pp.A4-HandOverHandLocking/src/main/java/pp/ThreadsafeSimplifiedList.java index cd9d9ce..8957eb5 100644 --- a/pp.A4-HandOverHandLocking/src/main/java/pp/ThreadsafeSimplifiedList.java +++ b/pp.A4-HandOverHandLocking/src/main/java/pp/ThreadsafeSimplifiedList.java @@ -1,19 +1,15 @@ package pp; -import lombok.Synchronized; -import net.jcip.annotations.ThreadSafe; - -@ThreadSafe public class ThreadsafeSimplifiedList implements SimplifiedList { private Node first; - private class Node { - private U element; - private Node prev; - private Node next; + private static class Node { + U element; + Node prev; + Node next; + final Object lock = new Object(); - private Node(U element, Node prev, Node next) { - super(); + Node(U element, Node prev, Node next) { this.element = element; this.prev = prev; this.next = next; @@ -21,78 +17,84 @@ public class ThreadsafeSimplifiedList implements SimplifiedList { } public ThreadsafeSimplifiedList() { - super(); this.first = null; } - /** - * Returns the element at the specified position in this list. - * - * @param index index of the element to return - * @return the element at the specified position in this list - */ @Override - @Synchronized - public T get(int index) { - var ptr = this.first; - for (var j = 0; j < index; j++) { - ptr = ptr.next; - } - return delay(ptr.element); - } - - /** - * Appends the specified element to the end of this list. There are no - * limitations on what elements may be added to this list. - * - * @param element element to be appended to this list - * @return true - * @see java.util.Collection#add(Object) - * - */ - @Override - @Synchronized public boolean add(T element) { - if (this.first != null) { - var ptr = this.first; - while (ptr.next != null) { - ptr = ptr.next; + synchronized (this) { + if (first == null) { + first = new Node<>(element, null, null); + return true; + } + } + + Node curr; + synchronized (first.lock) { + curr = first; + while (true) { + Node next; + synchronized (curr.lock) { + next = curr.next; + if (next == null) { + // letztes Element erreicht → neues anhängen + curr.next = new Node<>(element, curr, null); + return true; + } + } + synchronized (next.lock) { + curr = next; + } } - ptr.next = new Node<>(element, ptr, null); - } else { - this.first = new Node<>(element, null, null); } - return true; } - /** - * Replaces the element at the specified position in this list with the - * specified element. - * - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position - */ @Override - @Synchronized + public T get(int index) { + Node curr; + synchronized (this) { + if (first == null) throw new IndexOutOfBoundsException(); + curr = first; + } + + synchronized (curr.lock) { + for (int i = 0; i < index; i++) { + Node next = curr.next; + if (next == null) throw new IndexOutOfBoundsException(); + synchronized (next.lock) { + curr = next; + } + } + return delay(curr.element); + } + } + + @Override public T set(int index, T element) { - var ptr = this.first; - for (var j = 0; j < index; j++) { - ptr = ptr.next; + Node curr; + synchronized (this) { + if (first == null) throw new IndexOutOfBoundsException(); + curr = first; + } + + synchronized (curr.lock) { + for (int i = 0; i < index; i++) { + Node next = curr.next; + if (next == null) throw new IndexOutOfBoundsException(); + synchronized (next.lock) { + curr = next; + } + } + T old = curr.element; + curr.element = element; + return old; } - var prevElement = ptr.element; - ptr.element = element; - return prevElement; } - /** - * Returns true if this list contains no elements. - * - * @return true if this list contains no elements - */ @Override - @Synchronized public boolean isEmpty() { - return this.first == null; + synchronized (this) { + return first == null; + } } } -- 2.43.0