main
Obai Albek 2025-05-23 23:04:24 +02:00
parent 0d8f596ea1
commit 4074d08a1c
3 changed files with 14 additions and 2 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

@ -31,6 +31,11 @@ public class ThreadsafeSimplifiedList<T>
@Override @Override
public T get(int index){ public T get(int index){
this.listLock.lock(); this.listLock.lock();
if (this.head == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException("The list is empty");
}
var ptr = this.head; var ptr = this.head;
ptr.nodeLock.lock(); ptr.nodeLock.lock();
this.listLock.unlock(); this.listLock.unlock();
@ -91,6 +96,10 @@ 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();
if (this.head == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException("The list is empty");
}
var ptr = this.head; var ptr = this.head;
ptr.nodeLock.lock(); ptr.nodeLock.lock();
this.listLock.unlock(); this.listLock.unlock();