Finished writing ThreadsafeSimplifiedList

main
Yan Wittmann 2022-12-01 17:16:43 +01:00 committed by Skyball2000
parent bd28a1e622
commit 9144eeb067
2 changed files with 86 additions and 23 deletions

View File

@ -8,10 +8,14 @@ public class Main {
var sum = 0; var sum = 0;
for (var i = start; i < end; i++) { for (var i = start; i < end; i++) {
sum += list.get(i); sum += list.get(i);
if (i % 250 == 0) {
System.out.println("Progress in " + Thread.currentThread().getName() + ": " + sum + " " + i);
}
} }
if (sum != expected) { if (sum != expected) {
System.out.println("Fehler in " System.err.println("Fehler in " + Thread.currentThread().getName() + ": " + sum);
+ Thread.currentThread().getName() + ": " + sum); } else {
System.out.println("Ok in " + Thread.currentThread().getName() + ": " + sum);
} }
}; };
} }
@ -21,6 +25,9 @@ public class Main {
for (int i = 0; i < 5000; i++) { for (int i = 0; i < 5000; i++) {
list.add(i); list.add(i);
} }
System.out.println("Liste: " + list);
var thread0 = new Thread(sliceSum(0, 1250, 780625)); var thread0 = new Thread(sliceSum(0, 1250, 780625));
var thread1 = new Thread(sliceSum(1250, 2500, 2343125)); var thread1 = new Thread(sliceSum(1250, 2500, 2343125));
var thread2 = new Thread(sliceSum(2500, 3750, 3905625)); var thread2 = new Thread(sliceSum(2500, 3750, 3905625));

View File

@ -1,5 +1,8 @@
package io.dama.ffi.hoh; package io.dama.ffi.hoh;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> { public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
private Node<T> first; private Node<T> first;
@ -7,6 +10,7 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
private U element; private U element;
private Node<U> prev; private Node<U> prev;
private Node<U> next; private Node<U> next;
private final Lock lock = new ReentrantLock();
private Node(U element, Node<U> prev, Node<U> next) { private Node(U element, Node<U> prev, Node<U> next) {
super(); super();
@ -14,6 +18,18 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
this.prev = prev; this.prev = prev;
this.next = next; this.next = next;
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("'").append(this.element).append("'");
var node = this;
while (node.next != null) {
node = node.next;
builder.append(" -> '").append(node.element).append("'");
}
return builder.toString();
}
} }
public ThreadsafeSimplifiedList() { public ThreadsafeSimplifiedList() {
@ -28,12 +44,24 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @return the element at the specified position in this list * @return the element at the specified position in this list
*/ */
@Override @Override
public synchronized T get(int index) { public T get(int index) {
var ptr = this.first; Node<T> ptr = this.first;
ptr.lock.lock();
try {
for (var j = 0; j < index; j++) { for (var j = 0; j < index; j++) {
ptr.next.lock.lock();
ptr = ptr.next; ptr = ptr.next;
ptr.prev.lock.unlock();
} }
return delay(ptr.element); return delay(ptr.element);
} finally {
ptr.lock.unlock();
if (ptr.prev != null && ptr.prev.lock.tryLock()) {
ptr.prev.lock.unlock();
}
}
} }
/** /**
@ -43,16 +71,27 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @param element element to be appended to this list * @param element element to be appended to this list
* @return true * @return true
* @see java.util.Collection#add(Object) * @see java.util.Collection#add(Object)
*
*/ */
@Override @Override
public synchronized boolean add(T element) { public boolean add(T element) {
if (this.first != null) { if (this.first != null) {
var ptr = this.first; var ptr = this.first;
ptr.lock.lock();
try {
while (ptr.next != null) { while (ptr.next != null) {
ptr.next.lock.lock();
ptr = ptr.next; ptr = ptr.next;
ptr.prev.lock.unlock();
} }
ptr.next = new Node<>(element, ptr, null); ptr.next = new Node<>(element, ptr, null);
} finally {
ptr.lock.unlock();
if (ptr.prev != null && ptr.prev.lock.tryLock()) {
ptr.prev.lock.unlock();
}
}
} else { } else {
this.first = new Node<>(element, null, null); this.first = new Node<>(element, null, null);
} }
@ -68,14 +107,26 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @return the element previously at the specified position * @return the element previously at the specified position
*/ */
@Override @Override
public synchronized T set(int index, T element) { public T set(int index, T element) {
var ptr = this.first; Node<T> ptr = this.first;
ptr.lock.lock();
try {
for (var j = 0; j < index; j++) { for (var j = 0; j < index; j++) {
ptr.next.lock.lock();
ptr = ptr.next; ptr = ptr.next;
ptr.prev.lock.unlock();
} }
var prevElement = ptr.element; var prevElement = ptr.element;
ptr.element = element; ptr.element = element;
return prevElement; return prevElement;
} finally {
ptr.lock.unlock();
if (ptr.prev != null && ptr.prev.lock.tryLock()) {
ptr.prev.lock.unlock();
}
}
} }
/** /**
@ -84,7 +135,12 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @return true if this list contains no elements * @return true if this list contains no elements
*/ */
@Override @Override
public synchronized boolean isEmpty() { public boolean isEmpty() {
return this.first == null; return this.first == null;
} }
@Override
public String toString() {
return this.first != null ? "[" + this.first + "]" : "[]";
}
} }