Finished writing ThreadsafeSimplifiedList
parent
bd28a1e622
commit
9144eeb067
|
@ -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));
|
||||||
|
|
|
@ -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,31 +44,54 @@ 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;
|
||||||
for (var j = 0; j < index; j++) {
|
|
||||||
ptr = ptr.next;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the specified element to the end of this list. There are no
|
* Appends the specified element to the end of this list. There are no
|
||||||
* limitations on what elements may be added to this list.
|
* limitations on what elements may be added to this list.
|
||||||
*
|
*
|
||||||
* @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;
|
||||||
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 {
|
} else {
|
||||||
this.first = new Node<>(element, null, null);
|
this.first = new Node<>(element, null, null);
|
||||||
}
|
}
|
||||||
|
@ -62,29 +101,46 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
/**
|
/**
|
||||||
* Replaces the element at the specified position in this list with the
|
* Replaces the element at the specified position in this list with the
|
||||||
* specified element.
|
* specified element.
|
||||||
*
|
*
|
||||||
* @param index index of the element to replace
|
* @param index index of the element to replace
|
||||||
* @param element element to be stored at the specified position
|
* @param element element to be stored at the specified position
|
||||||
* @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;
|
||||||
for (var j = 0; j < index; j++) {
|
|
||||||
ptr = ptr.next;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this list contains no elements.
|
* Returns true if this list contains no elements.
|
||||||
*
|
*
|
||||||
* @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 + "]" : "[]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue