Compare commits

..

No commits in common. "25297b686fe43cf2e93cb3cf6e4aead6ec956816" and "bde868ad4746fca3a867774a0bc5b2594b998c26" have entirely different histories.

2 changed files with 86 additions and 133 deletions

View File

@ -18,25 +18,25 @@ public class Main {
} }
public static void main(String... args) throws InterruptedException { public static void main(String... args) throws InterruptedException {
list = new ThreadsafeSimplifiedList<>(); list = new ThreadsafeSimplifiedList<>();
for (int i = 0; i < 5000; i++) { for (int i = 0; i < 5000; i++) {
list.add(i); list.add(i);
} }
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));
var thread3 = new Thread(sliceSum(3750, 5000, 5468125)); var thread3 = new Thread(sliceSum(3750, 5000, 5468125));
var start = System.currentTimeMillis(); var start = System.currentTimeMillis();
thread0.start(); thread0.start();
thread1.start(); thread1.start();
thread2.start(); thread2.start();
thread3.start(); thread3.start();
thread0.join(); thread0.join();
thread1.join(); thread1.join();
thread2.join(); thread2.join();
thread3.join(); thread3.join();
System.out.printf("%s: %d ms\n", list.getClass().toString(), System.out.printf("%s: %d ms\n", list.getClass().toString(),
System.currentTimeMillis() - start); System.currentTimeMillis() - start);
} }
} }

View File

@ -1,144 +1,97 @@
package pp; package pp;
import java.util.concurrent.locks.Lock; public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
import java.util.concurrent.locks.ReentrantLock; private Node<T> first;
private final Object headLock = new Object();
public class ThreadsafeSimplifiedList<T> private static class Node<U> {
implements SimplifiedList<T> { U element;
Node<U> prev;
Node<U> next;
final Object lock = new Object();
private Node<T> head; Node(U element, Node<U> prev, Node<U> next) {
private final Lock listLock = new ReentrantLock();
private class Node<U> {
private U element;
private Node<U> next;
private final Lock nodeLock;
private Node(U element, Node<U> prev, Node<U> next) {
super();
this.element = element; this.element = element;
this.prev = prev;
this.next = next; this.next = next;
this.nodeLock = new ReentrantLock();
} }
} }
/** public ThreadsafeSimplifiedList() {
* Returns the element at the specified position in this list. this.first = null;
*
* @param index index of the element to return
* @return the element at the specified position in this list
*/
@Override
public T get(int index) {
this.listLock.lock();
var ptr = this.head;
if (ptr == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
}
try {
return delay(ptr.element);
} finally {
ptr.nodeLock.unlock();
}
} }
/**
* 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 @Override
public boolean add(T element) { public boolean add(T element) {
this.listLock.lock(); Node<T> curr;
if (this.head != null) {
var ptr = this.head; synchronized (headLock) {
ptr.nodeLock.lock(); if (first == null) {
this.listLock.unlock(); first = new Node<>(element, null, null);
while (ptr.next != null) { return true;
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} }
ptr.next = new Node<>(element, ptr, null); curr = first;
ptr.nodeLock.unlock(); }
} else {
this.head = new Node<>(element, null, null); while (true) {
this.listLock.unlock(); Node<T> next;
synchronized (curr.lock) {
next = curr.next;
if (next == null) {
curr.next = new Node<>(element, curr, null);
return true;
}
}
curr = next;
}
}
@Override
public T get(int index) {
Node<T> curr;
synchronized (headLock) {
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);
} }
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 @Override
public T set(int index, T element) { public T set(int index, T element) {
this.listLock.lock(); Node<T> curr;
var ptr = this.head; synchronized (headLock) {
if (ptr == null) { if (first == null) throw new IndexOutOfBoundsException();
this.listLock.unlock(); curr = first;
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
} }
try { synchronized (curr.lock) {
T old = ptr.element; for (int i = 0; i < index; i++) {
ptr.element = element; 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; return old;
} finally {
ptr.nodeLock.unlock();
} }
} }
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
this.listLock.lock(); synchronized (headLock) {
try { return first == null;
return this.head == null;
} finally {
this.listLock.unlock();
} }
} }
} }