Compare commits

..

3 Commits

Author SHA1 Message Date
Obai Albek 65f4340099 final Version 2025-05-23 23:34:51 +02:00
Obai Albek 4074d08a1c Update 2025-05-23 23:04:24 +02:00
Obai Albek 0d8f596ea1 final Version 2025-05-23 15:57:07 +02:00
3 changed files with 121 additions and 127 deletions

View File

@ -13,7 +13,8 @@ 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,6 +7,7 @@ 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);
@ -28,6 +29,7 @@ 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

@ -29,13 +29,14 @@ public class ThreadsafeSimplifiedList<T>
* @return the element at the specified position in this list * @return the element at the specified position in this list
*/ */
@Override @Override
public T get(int index) { public T get(int index){
this.listLock.lock(); this.listLock.lock();
var ptr = this.head; if (this.head == null) {
if (ptr == null) {
this.listLock.unlock(); this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)"); throw new IndexOutOfBoundsException("The list is empty");
} }
var ptr = this.head;
ptr.nodeLock.lock(); ptr.nodeLock.lock();
this.listLock.unlock(); this.listLock.unlock();
for (var i = 0; i < index; i++) { for (var i = 0; i < index; i++) {
@ -45,16 +46,12 @@ public class ThreadsafeSimplifiedList<T>
ptr = ptr.next; ptr = ptr.next;
savePtr.nodeLock.unlock(); savePtr.nodeLock.unlock();
} else { } else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds"); throw new IndexOutOfBoundsException(index + " out of bounds");
} }
} }
try { try {return delay(ptr.element);}
return delay(ptr.element); finally { ptr.nodeLock.unlock();}
} finally {
ptr.nodeLock.unlock();
}
} }
/** /**
@ -99,11 +96,11 @@ public class ThreadsafeSimplifiedList<T>
@Override @Override
public T set(int index, T element) { public T set(int index, T element) {
this.listLock.lock(); this.listLock.lock();
var ptr = this.head; if (this.head == null) {
if (ptr == null) {
this.listLock.unlock(); this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)"); throw new IndexOutOfBoundsException("The list is empty");
} }
var ptr = this.head;
ptr.nodeLock.lock(); ptr.nodeLock.lock();
this.listLock.unlock(); this.listLock.unlock();
for (var i = 0; i < index; i++) { for (var i = 0; i < index; i++) {
@ -112,16 +109,13 @@ public class ThreadsafeSimplifiedList<T>
var savePtr = ptr; var savePtr = ptr;
ptr = ptr.next; ptr = ptr.next;
savePtr.nodeLock.unlock(); savePtr.nodeLock.unlock();
} else { } else
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds"); throw new IndexOutOfBoundsException(index + " out of bounds");
}
}
}
try { try {
T old = ptr.element;
ptr.element = element; ptr.element = element;
return old; return element;
} finally { } finally {
ptr.nodeLock.unlock(); ptr.nodeLock.unlock();
} }
@ -135,10 +129,7 @@ public class ThreadsafeSimplifiedList<T>
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
this.listLock.lock(); this.listLock.lock();
try { try { return this.head == null; }
return this.head == null; finally { this.listLock.unlock();}
} finally {
this.listLock.unlock();
}
} }
} }