Compare commits
No commits in common. "d37c6d81e0843f89cbfddf7d8eb3980e347ae690" and "ae25cc3a83f40f49dbcfced14c48cbf1a2966f4d" have entirely different histories.
d37c6d81e0
...
ae25cc3a83
|
@ -1,6 +1,5 @@
|
||||||
package pp;
|
package pp;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static SimplifiedList<Integer> list;
|
static SimplifiedList<Integer> list;
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
package pp;
|
package pp;
|
||||||
|
|
||||||
|
import lombok.Synchronized;
|
||||||
|
import net.jcip.annotations.ThreadSafe;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
private Node<T> first;
|
private Node<T> first;
|
||||||
|
|
||||||
private static class Node<U> {
|
private class Node<U> {
|
||||||
U element;
|
private U element;
|
||||||
Node<U> prev;
|
private Node<U> prev;
|
||||||
Node<U> next;
|
private Node<U> next;
|
||||||
final Object lock = new Object();
|
|
||||||
|
|
||||||
Node(U element, Node<U> prev, Node<U> next) {
|
private Node(U element, Node<U> prev, Node<U> next) {
|
||||||
|
super();
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.prev = prev;
|
this.prev = prev;
|
||||||
this.next = next;
|
this.next = next;
|
||||||
|
@ -17,84 +21,78 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThreadsafeSimplifiedList() {
|
public ThreadsafeSimplifiedList() {
|
||||||
|
super();
|
||||||
this.first = null;
|
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
|
@Override
|
||||||
public boolean add(T element) {
|
@Synchronized
|
||||||
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) {
|
public T get(int index) {
|
||||||
Node<T> curr;
|
var ptr = this.first;
|
||||||
synchronized (this) {
|
for (var j = 0; j < index; j++) {
|
||||||
if (first == null) throw new IndexOutOfBoundsException();
|
ptr = ptr.next;
|
||||||
curr = first;
|
}
|
||||||
}
|
return delay(ptr.element);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
@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);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public T set(int index, T element) {
|
||||||
Node<T> curr;
|
var ptr = this.first;
|
||||||
synchronized (this) {
|
for (var j = 0; j < index; j++) {
|
||||||
if (first == null) throw new IndexOutOfBoundsException();
|
ptr = ptr.next;
|
||||||
curr = first;
|
}
|
||||||
}
|
var prevElement = ptr.element;
|
||||||
|
ptr.element = element;
|
||||||
synchronized (curr.lock) {
|
return prevElement;
|
||||||
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
|
@Override
|
||||||
|
@Synchronized
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
synchronized (this) {
|
return this.first == null;
|
||||||
return first == null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue