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