final
parent
9982245715
commit
25297b686f
|
@ -3,32 +3,25 @@ package pp;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import net.jcip.annotations.ThreadSafe;
|
public class ThreadsafeSimplifiedList<T>
|
||||||
|
implements SimplifiedList<T> {
|
||||||
|
|
||||||
@ThreadSafe
|
private Node<T> head;
|
||||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
|
||||||
private Node<T> first;
|
|
||||||
private final Lock listLock = new ReentrantLock();
|
private final Lock listLock = new ReentrantLock();
|
||||||
|
|
||||||
private class Node<U> {
|
private class Node<U> {
|
||||||
private U element;
|
private U element;
|
||||||
private Node<U> prev;
|
|
||||||
private Node<U> next;
|
private Node<U> next;
|
||||||
private final Lock lock = new ReentrantLock();
|
private final Lock nodeLock;
|
||||||
|
|
||||||
private Node(U element, Node<U> prev, Node<U> next) {
|
private Node(U element, Node<U> prev, Node<U> next) {
|
||||||
super();
|
super();
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.prev = prev;
|
|
||||||
this.next = next;
|
this.next = next;
|
||||||
|
this.nodeLock = new ReentrantLock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThreadsafeSimplifiedList() {
|
|
||||||
super();
|
|
||||||
this.first = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element at the specified position in this list.
|
* Returns the element at the specified position in this list.
|
||||||
*
|
*
|
||||||
|
@ -38,23 +31,29 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
@Override
|
@Override
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
this.listLock.lock();
|
this.listLock.lock();
|
||||||
var ptr = this.first;
|
var ptr = this.head;
|
||||||
ptr.lock.lock();
|
if (ptr == null) {
|
||||||
this.listLock.unlock();
|
this.listLock.unlock();
|
||||||
for (var j = 0; j < index; j++) {
|
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) {
|
if (ptr.next != null) {
|
||||||
ptr.next.lock.lock();
|
ptr.next.nodeLock.lock();
|
||||||
var savePtr = ptr;
|
var savePtr = ptr;
|
||||||
ptr = ptr.next;
|
ptr = ptr.next;
|
||||||
savePtr.lock.unlock();
|
savePtr.nodeLock.unlock();
|
||||||
} else {
|
} else {
|
||||||
|
ptr.nodeLock.unlock();
|
||||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return delay(ptr.element);
|
return delay(ptr.element);
|
||||||
} finally {
|
} finally {
|
||||||
ptr.lock.unlock();
|
ptr.nodeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,19 +69,20 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
@Override
|
@Override
|
||||||
public boolean add(T element) {
|
public boolean add(T element) {
|
||||||
this.listLock.lock();
|
this.listLock.lock();
|
||||||
if (this.first != null) {
|
if (this.head != null) {
|
||||||
var ptr = this.first;
|
var ptr = this.head;
|
||||||
ptr.lock.lock();
|
ptr.nodeLock.lock();
|
||||||
this.listLock.unlock();
|
this.listLock.unlock();
|
||||||
while (ptr.next != null) {
|
while (ptr.next != null) {
|
||||||
ptr.next.lock.lock();
|
ptr.next.nodeLock.lock();
|
||||||
var saveNode = ptr;
|
var savePtr = ptr;
|
||||||
ptr = ptr.next;
|
ptr = ptr.next;
|
||||||
saveNode.lock.unlock();
|
savePtr.nodeLock.unlock();
|
||||||
}
|
}
|
||||||
ptr.next = new Node<>(element, ptr, null);
|
ptr.next = new Node<>(element, ptr, null);
|
||||||
|
ptr.nodeLock.unlock();
|
||||||
} else {
|
} else {
|
||||||
this.first = new Node<>(element, null, null);
|
this.head = new Node<>(element, null, null);
|
||||||
this.listLock.unlock();
|
this.listLock.unlock();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -99,25 +99,31 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
@Override
|
@Override
|
||||||
public T set(int index, T element) {
|
public T set(int index, T element) {
|
||||||
this.listLock.lock();
|
this.listLock.lock();
|
||||||
var ptr = this.first;
|
var ptr = this.head;
|
||||||
ptr.lock.lock();
|
if (ptr == null) {
|
||||||
this.listLock.unlock();
|
this.listLock.unlock();
|
||||||
for (var j = 0; j < index; j++) {
|
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) {
|
if (ptr.next != null) {
|
||||||
ptr.next.lock.lock();
|
ptr.next.nodeLock.lock();
|
||||||
var saveNode = ptr;
|
var savePtr = ptr;
|
||||||
ptr = ptr.next;
|
ptr = ptr.next;
|
||||||
saveNode.lock.unlock();
|
savePtr.nodeLock.unlock();
|
||||||
} else {
|
} else {
|
||||||
|
ptr.nodeLock.unlock();
|
||||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var prevElement = ptr.element;
|
T old = ptr.element;
|
||||||
ptr.element = element;
|
ptr.element = element;
|
||||||
return prevElement;
|
return old;
|
||||||
} finally {
|
} finally {
|
||||||
ptr.lock.unlock();
|
ptr.nodeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,10 +136,9 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
this.listLock.lock();
|
this.listLock.lock();
|
||||||
try {
|
try {
|
||||||
return this.first == null;
|
return this.head == null;
|
||||||
} finally {
|
} finally {
|
||||||
this.listLock.unlock();
|
this.listLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue