Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
25297b686f |
|
|
@ -13,8 +13,7 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ 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);
|
||||
|
||||
|
|
@ -29,7 +28,6 @@ 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);
|
||||
|
||||
|
|
|
|||
|
|
@ -29,14 +29,13 @@ public class ThreadsafeSimplifiedList<T>
|
|||
* @return the element at the specified position in this list
|
||||
*/
|
||||
@Override
|
||||
public T get(int index){
|
||||
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;
|
||||
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++) {
|
||||
|
|
@ -46,12 +45,16 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -96,11 +99,11 @@ public class ThreadsafeSimplifiedList<T>
|
|||
@Override
|
||||
public T set(int index, T element) {
|
||||
this.listLock.lock();
|
||||
if (this.head == null) {
|
||||
this.listLock.unlock();
|
||||
throw new IndexOutOfBoundsException("The list is empty");
|
||||
}
|
||||
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++) {
|
||||
|
|
@ -109,13 +112,16 @@ public class ThreadsafeSimplifiedList<T>
|
|||
var savePtr = ptr;
|
||||
ptr = ptr.next;
|
||||
savePtr.nodeLock.unlock();
|
||||
} else
|
||||
} else {
|
||||
ptr.nodeLock.unlock();
|
||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
T old = ptr.element;
|
||||
ptr.element = element;
|
||||
return element;
|
||||
return old;
|
||||
} finally {
|
||||
ptr.nodeLock.unlock();
|
||||
}
|
||||
|
|
@ -129,7 +135,10 @@ 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue