first test

everything work fine (for now)
pull/2/head
Shahnam Javidnia 2025-05-20 16:43:10 +02:00
parent ae25cc3a83
commit 00b8601208
2 changed files with 70 additions and 67 deletions

View File

@ -1,5 +1,6 @@
package pp; package pp;
public class Main { public class Main {
static SimplifiedList<Integer> list; static SimplifiedList<Integer> list;

View File

@ -1,19 +1,15 @@
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 class Node<U> { private static class Node<U> {
private U element; U element;
private Node<U> prev; Node<U> prev;
private Node<U> next; Node<U> next;
final Object lock = new Object();
private Node(U element, Node<U> prev, Node<U> next) { 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;
@ -21,78 +17,84 @@ 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
@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) { public boolean add(T element) {
if (this.first != null) { synchronized (this) {
var ptr = this.first; if (first == null) {
while (ptr.next != null) { first = new Node<>(element, null, null);
ptr = ptr.next;
}
ptr.next = new Node<>(element, ptr, null);
} else {
this.first = new Node<>(element, null, null);
}
return true; 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 @Override
@Synchronized
public T set(int index, T element) { public T set(int index, T element) {
var ptr = this.first; Node<T> curr;
for (var j = 0; j < index; j++) { synchronized (this) {
ptr = ptr.next; if (first == null) throw new IndexOutOfBoundsException();
} curr = first;
var prevElement = ptr.element; }
ptr.element = element;
return prevElement; 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 @Override
@Synchronized
public boolean isEmpty() { public boolean isEmpty() {
return this.first == null; synchronized (this) {
return first == null;
}
} }
} }