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;
for (var i = start; i < end; i++) {
sum += list.get(i);
if (i % 250 == 0) {
System.out.println("Progress in " + Thread.currentThread().getName() + ": " + sum + " " + i);
}
}
if (sum != expected) {
System.out.println("Fehler in "
+ Thread.currentThread().getName() + ": " + sum);
System.err.println("Fehler in " + 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++) {
list.add(i);
}
System.out.println("Liste: " + list);
var thread0 = new Thread(sliceSum(0, 1250, 780625));
var thread1 = new Thread(sliceSum(1250, 2500, 2343125));
var thread2 = new Thread(sliceSum(2500, 3750, 3905625));

View File

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