commit
d37c6d81e0
|
@ -1,5 +1,6 @@
|
|||
package pp;
|
||||
|
||||
|
||||
public class Main {
|
||||
static SimplifiedList<Integer> list;
|
||||
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
package pp;
|
||||
|
||||
import lombok.Synchronized;
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||
private Node<T> first;
|
||||
|
||||
private class Node<U> {
|
||||
private U element;
|
||||
private Node<U> prev;
|
||||
private Node<U> next;
|
||||
private static class Node<U> {
|
||||
U element;
|
||||
Node<U> prev;
|
||||
Node<U> next;
|
||||
final Object lock = new Object();
|
||||
|
||||
private Node(U element, Node<U> prev, Node<U> next) {
|
||||
super();
|
||||
Node(U element, Node<U> prev, Node<U> next) {
|
||||
this.element = element;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
|
@ -21,78 +17,84 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
|||
}
|
||||
|
||||
public ThreadsafeSimplifiedList() {
|
||||
super();
|
||||
this.first = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index index of the element to return
|
||||
* @return the element at the specified position in this list
|
||||
*/
|
||||
@Override
|
||||
@Synchronized
|
||||
public T get(int index) {
|
||||
var ptr = this.first;
|
||||
for (var j = 0; j < index; j++) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
return delay(ptr.element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the specified element to the end of this list. There are no
|
||||
* limitations on what elements may be added to this list.
|
||||
*
|
||||
* @param element element to be appended to this list
|
||||
* @return true
|
||||
* @see java.util.Collection#add(Object)
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
@Synchronized
|
||||
public boolean add(T element) {
|
||||
if (this.first != null) {
|
||||
var ptr = this.first;
|
||||
while (ptr.next != null) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
ptr.next = new Node<>(element, ptr, null);
|
||||
} else {
|
||||
this.first = new Node<>(element, null, null);
|
||||
}
|
||||
synchronized (this) {
|
||||
if (first == null) {
|
||||
first = new Node<>(element, null, null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Node<T> curr;
|
||||
synchronized (first.lock) {
|
||||
curr = first;
|
||||
while (true) {
|
||||
Node<T> next;
|
||||
synchronized (curr.lock) {
|
||||
next = curr.next;
|
||||
if (next == null) {
|
||||
// letztes Element erreicht → neues anhängen
|
||||
curr.next = new Node<>(element, curr, null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
synchronized (next.lock) {
|
||||
curr = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int index) {
|
||||
Node<T> curr;
|
||||
synchronized (this) {
|
||||
if (first == null) throw new IndexOutOfBoundsException();
|
||||
curr = first;
|
||||
}
|
||||
|
||||
synchronized (curr.lock) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
Node<T> next = curr.next;
|
||||
if (next == null) throw new IndexOutOfBoundsException();
|
||||
synchronized (next.lock) {
|
||||
curr = next;
|
||||
}
|
||||
}
|
||||
return delay(curr.element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position in this list with the
|
||||
* specified element.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@Override
|
||||
@Synchronized
|
||||
public T set(int index, T element) {
|
||||
var ptr = this.first;
|
||||
for (var j = 0; j < index; j++) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
var prevElement = ptr.element;
|
||||
ptr.element = element;
|
||||
return prevElement;
|
||||
Node<T> curr;
|
||||
synchronized (this) {
|
||||
if (first == null) throw new IndexOutOfBoundsException();
|
||||
curr = first;
|
||||
}
|
||||
|
||||
synchronized (curr.lock) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
Node<T> next = curr.next;
|
||||
if (next == null) throw new IndexOutOfBoundsException();
|
||||
synchronized (next.lock) {
|
||||
curr = next;
|
||||
}
|
||||
}
|
||||
T old = curr.element;
|
||||
curr.element = element;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this list contains no elements.
|
||||
*
|
||||
* @return true if this list contains no elements
|
||||
*/
|
||||
@Override
|
||||
@Synchronized
|
||||
public boolean isEmpty() {
|
||||
return this.first == null;
|
||||
synchronized (this) {
|
||||
return first == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue