Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
65f4340099 | |
|
4074d08a1c | |
|
0d8f596ea1 |
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -3,32 +3,25 @@ package pp;
|
|||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
public class ThreadsafeSimplifiedList<T>
|
||||
implements SimplifiedList<T> {
|
||||
|
||||
@ThreadSafe
|
||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||
private Node<T> first;
|
||||
private Node<T> head;
|
||||
private final Lock listLock = new ReentrantLock();
|
||||
|
||||
private class Node<U> {
|
||||
private U element;
|
||||
private Node<U> prev;
|
||||
private Node<U> next;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
private final Lock nodeLock;
|
||||
|
||||
private Node(U element, Node<U> prev, Node<U> next) {
|
||||
super();
|
||||
this.element = element;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
this.nodeLock = new ReentrantLock();
|
||||
}
|
||||
}
|
||||
|
||||
public ThreadsafeSimplifiedList() {
|
||||
super();
|
||||
this.first = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
|
@ -36,26 +29,29 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<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();
|
||||
var ptr = this.first;
|
||||
ptr.lock.lock();
|
||||
if (this.head == null) {
|
||||
this.listLock.unlock();
|
||||
for (var j = 0; j < index; j++) {
|
||||
throw new IndexOutOfBoundsException("The list is empty");
|
||||
}
|
||||
|
||||
var ptr = this.head;
|
||||
ptr.nodeLock.lock();
|
||||
this.listLock.unlock();
|
||||
for (var i = 0; i < index; i++) {
|
||||
if (ptr.next != null) {
|
||||
ptr.next.lock.lock();
|
||||
ptr.next.nodeLock.lock();
|
||||
var savePtr = ptr;
|
||||
ptr = ptr.next;
|
||||
savePtr.lock.unlock();
|
||||
savePtr.nodeLock.unlock();
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||
}
|
||||
}
|
||||
try {
|
||||
return delay(ptr.element);
|
||||
} finally {
|
||||
ptr.lock.unlock();
|
||||
}
|
||||
|
||||
try {return delay(ptr.element);}
|
||||
finally { ptr.nodeLock.unlock();}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,19 +66,20 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
|||
@Override
|
||||
public boolean add(T element) {
|
||||
this.listLock.lock();
|
||||
if (this.first != null) {
|
||||
var ptr = this.first;
|
||||
ptr.lock.lock();
|
||||
if (this.head != null) {
|
||||
var ptr = this.head;
|
||||
ptr.nodeLock.lock();
|
||||
this.listLock.unlock();
|
||||
while (ptr.next != null) {
|
||||
ptr.next.lock.lock();
|
||||
var saveNode = ptr;
|
||||
ptr.next.nodeLock.lock();
|
||||
var savePtr = ptr;
|
||||
ptr = ptr.next;
|
||||
saveNode.lock.unlock();
|
||||
savePtr.nodeLock.unlock();
|
||||
}
|
||||
ptr.next = new Node<>(element, ptr, null);
|
||||
ptr.nodeLock.unlock();
|
||||
} else {
|
||||
this.first = new Node<>(element, null, null);
|
||||
this.head = new Node<>(element, null, null);
|
||||
this.listLock.unlock();
|
||||
}
|
||||
return true;
|
||||
|
@ -99,25 +96,28 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
|||
@Override
|
||||
public T set(int index, T element) {
|
||||
this.listLock.lock();
|
||||
var ptr = this.first;
|
||||
ptr.lock.lock();
|
||||
if (this.head == null) {
|
||||
this.listLock.unlock();
|
||||
for (var j = 0; j < index; j++) {
|
||||
if (ptr.next != null) {
|
||||
ptr.next.lock.lock();
|
||||
var saveNode = ptr;
|
||||
ptr = ptr.next;
|
||||
saveNode.lock.unlock();
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||
throw new IndexOutOfBoundsException("The list is empty");
|
||||
}
|
||||
var ptr = this.head;
|
||||
ptr.nodeLock.lock();
|
||||
this.listLock.unlock();
|
||||
for (var i = 0; i < index; i++) {
|
||||
if (ptr.next != null) {
|
||||
ptr.next.nodeLock.lock();
|
||||
var savePtr = ptr;
|
||||
ptr = ptr.next;
|
||||
savePtr.nodeLock.unlock();
|
||||
} else
|
||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||
|
||||
}
|
||||
try {
|
||||
var prevElement = ptr.element;
|
||||
ptr.element = element;
|
||||
return prevElement;
|
||||
return element;
|
||||
} finally {
|
||||
ptr.lock.unlock();
|
||||
ptr.nodeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -129,11 +129,7 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
|||
@Override
|
||||
public boolean isEmpty() {
|
||||
this.listLock.lock();
|
||||
try {
|
||||
return this.first == null;
|
||||
} finally {
|
||||
this.listLock.unlock();
|
||||
}
|
||||
try { return this.head == null; }
|
||||
finally { this.listLock.unlock();}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue