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) {
System.out.println("Fehler in "
+ 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
* @return the element at the specified position in this list
* @throws Exception
*/
public T get(int index);
@ -28,6 +29,7 @@ public interface SimplifiedList<T> {
* @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
* @throws Exception
*/
public T set(int index, T element);

View File

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