Compare commits

..

2 Commits

Author SHA1 Message Date
Shahnam Javidnia d37c6d81e0 Merge pull request 'first test' (#2) from SL_U2_SJ into main
Reviewed-on: #2
2025-05-21 11:12:55 +02:00
Shahnam Javidnia 00b8601208 first test
everything work fine (for now)
2025-05-20 16:43:10 +02:00
2 changed files with 70 additions and 67 deletions

View File

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

View File

@ -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;
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;
}
}
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 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);
}
}
@Override
public T set(int index, T element) {
var ptr = this.first;
for (var j = 0; j < index; j++) {
ptr = ptr.next;
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;
}
var prevElement = ptr.element;
ptr.element = element;
return prevElement;
}
/**
* 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;
}
}
}