Compare commits

..

1 Commits

Author SHA1 Message Date
Shahnam Javidnia 25297b686f final 2025-05-23 17:52:27 +02:00
3 changed files with 127 additions and 121 deletions

View File

@ -13,8 +13,7 @@ public class Main {
if (sum != expected) { if (sum != expected) {
System.out.println("Fehler in " System.out.println("Fehler in "
+ Thread.currentThread().getName() + ": " + sum); + Thread.currentThread().getName() + ": " + sum);
}else }
System.out.println("Kein Fehler in " + Thread.currentThread().getName() + ": " + sum);
}; };
} }

View File

@ -7,7 +7,6 @@ public interface SimplifiedList<T> {
* *
* @param index index of the element to return * @param index index of the element to return
* @return the element at the specified position in this list * @return the element at the specified position in this list
* @throws Exception
*/ */
public T get(int index); public T get(int index);
@ -29,7 +28,6 @@ public interface SimplifiedList<T> {
* @param index index of the element to replace * @param index index of the element to replace
* @param element element to be stored at the specified position * @param element element to be stored at the specified position
* @return the element previously at the specified position * @return the element previously at the specified position
* @throws Exception
*/ */
public T set(int index, T element); public T set(int index, T element);

View File

@ -6,130 +6,139 @@ import java.util.concurrent.locks.ReentrantLock;
public class ThreadsafeSimplifiedList<T> public class ThreadsafeSimplifiedList<T>
implements SimplifiedList<T> { implements SimplifiedList<T> {
private Node<T> head; private Node<T> head;
private final Lock listLock = new ReentrantLock(); private final Lock listLock = new ReentrantLock();
private class Node<U> { private class Node<U> {
private U element; private U element;
private Node<U> next; private Node<U> next;
private final Lock nodeLock; private final Lock nodeLock;
private Node(U element, Node<U> prev, Node<U> next) { private Node(U element, Node<U> prev, Node<U> next) {
super(); super();
this.element = element; this.element = element;
this.next = next; this.next = next;
this.nodeLock = new ReentrantLock(); this.nodeLock = new ReentrantLock();
} }
}
/**
* 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
public T get(int index) {
this.listLock.lock();
var ptr = this.head;
if (ptr == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
} }
/** try {
* Returns the element at the specified position in this list. return delay(ptr.element);
* } finally {
* @param index index of the element to return ptr.nodeLock.unlock();
* @return the element at the specified position in this list }
*/ }
@Override
public T get(int index){
this.listLock.lock();
if (this.head == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException("The list is empty");
}
var ptr = this.head; /**
ptr.nodeLock.lock(); * Appends the specified element to the end of this list. There are no
this.listLock.unlock(); * limitations on what elements may be added to this list.
for (var i = 0; i < index; i++) { *
if (ptr.next != null) { * @param element element to be appended to this list
ptr.next.nodeLock.lock(); * @return true
var savePtr = ptr; * @see java.util.Collection#add(Object)
ptr = ptr.next; *
savePtr.nodeLock.unlock(); */
} else { @Override
throw new IndexOutOfBoundsException(index + " out of bounds"); public boolean add(T element) {
} this.listLock.lock();
} if (this.head != null) {
var ptr = this.head;
ptr.nodeLock.lock();
this.listLock.unlock();
while (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
}
ptr.next = new Node<>(element, ptr, null);
ptr.nodeLock.unlock();
} else {
this.head = new Node<>(element, null, null);
this.listLock.unlock();
}
return true;
}
try {return delay(ptr.element);} /**
finally { ptr.nodeLock.unlock();} * 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
public T set(int index, T element) {
this.listLock.lock();
var ptr = this.head;
if (ptr == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
} }
/** try {
* Appends the specified element to the end of this list. There are no T old = ptr.element;
* limitations on what elements may be added to this list. ptr.element = element;
* return old;
* @param element element to be appended to this list } finally {
* @return true ptr.nodeLock.unlock();
* @see java.util.Collection#add(Object)
*
*/
@Override
public boolean add(T element) {
this.listLock.lock();
if (this.head != null) {
var ptr = this.head;
ptr.nodeLock.lock();
this.listLock.unlock();
while (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
}
ptr.next = new Node<>(element, ptr, null);
ptr.nodeLock.unlock();
} else {
this.head = new Node<>(element, null, null);
this.listLock.unlock();
}
return true;
} }
}
/** /**
* Replaces the element at the specified position in this list with the * Returns true if this list contains no elements.
* specified element. *
* * @return true if this list contains no elements
* @param index index of the element to replace */
* @param element element to be stored at the specified position @Override
* @return the element previously at the specified position public boolean isEmpty() {
*/ this.listLock.lock();
@Override try {
public T set(int index, T element) { return this.head == null;
this.listLock.lock(); } finally {
if (this.head == null) { this.listLock.unlock();
this.listLock.unlock();
throw new IndexOutOfBoundsException("The list is empty");
}
var ptr = this.head;
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else
throw new IndexOutOfBoundsException(index + " out of bounds");
}
try {
ptr.element = element;
return element;
} finally {
ptr.nodeLock.unlock();
}
}
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
@Override
public boolean isEmpty() {
this.listLock.lock();
try { return this.head == null; }
finally { this.listLock.unlock();}
} }
}
} }