final Version
parent
9982245715
commit
0d8f596ea1
|
@ -3,137 +3,121 @@ 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 final Lock listLock = new ReentrantLock();
|
||||||
private Node<T> first;
|
|
||||||
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 nodeLock;
|
||||||
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();
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.prev = prev;
|
this.next = next;
|
||||||
this.next = next;
|
this.nodeLock = new ReentrantLock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThreadsafeSimplifiedList() {
|
/**
|
||||||
super();
|
* 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;
|
||||||
|
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 {
|
||||||
|
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {return delay(ptr.element);}
|
||||||
|
finally { ptr.nodeLock.unlock();}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element at the specified position in this list.
|
* Appends the specified element to the end of this list. There are no
|
||||||
*
|
* limitations on what elements may be added to this list.
|
||||||
* @param index index of the element to return
|
*
|
||||||
* @return the element at the specified position in this list
|
* @param element element to be appended to this list
|
||||||
*/
|
* @return true
|
||||||
@Override
|
* @see java.util.Collection#add(Object)
|
||||||
public T get(int index) {
|
*
|
||||||
this.listLock.lock();
|
*/
|
||||||
var ptr = this.first;
|
@Override
|
||||||
ptr.lock.lock();
|
public boolean add(T element) {
|
||||||
this.listLock.unlock();
|
this.listLock.lock();
|
||||||
for (var j = 0; j < index; j++) {
|
if (this.head != null) {
|
||||||
if (ptr.next != null) {
|
var ptr = this.head;
|
||||||
ptr.next.lock.lock();
|
ptr.nodeLock.lock();
|
||||||
var savePtr = ptr;
|
this.listLock.unlock();
|
||||||
ptr = ptr.next;
|
while (ptr.next != null) {
|
||||||
savePtr.lock.unlock();
|
ptr.next.nodeLock.lock();
|
||||||
} else {
|
var savePtr = ptr;
|
||||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
ptr = ptr.next;
|
||||||
}
|
savePtr.nodeLock.unlock();
|
||||||
}
|
}
|
||||||
try {
|
ptr.next = new Node<>(element, ptr, null);
|
||||||
return delay(ptr.element);
|
ptr.nodeLock.unlock();
|
||||||
} finally {
|
} else {
|
||||||
ptr.lock.unlock();
|
this.head = new Node<>(element, null, null);
|
||||||
}
|
this.listLock.unlock();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the specified element to the end of this list. There are no
|
* Replaces the element at the specified position in this list with the
|
||||||
* limitations on what elements may be added to this list.
|
* specified element.
|
||||||
*
|
*
|
||||||
* @param element element to be appended to this list
|
* @param index index of the element to replace
|
||||||
* @return true
|
* @param element element to be stored at the specified position
|
||||||
* @see java.util.Collection#add(Object)
|
* @return the element previously at the specified position
|
||||||
*
|
*/
|
||||||
*/
|
@Override
|
||||||
@Override
|
public T set(int index, T element) {
|
||||||
public boolean add(T element) {
|
this.listLock.lock();
|
||||||
this.listLock.lock();
|
var ptr = this.head;
|
||||||
if (this.first != null) {
|
ptr.nodeLock.lock();
|
||||||
var ptr = this.first;
|
this.listLock.unlock();
|
||||||
ptr.lock.lock();
|
for (var i = 0; i < index; i++) {
|
||||||
this.listLock.unlock();
|
if (ptr.next != null) {
|
||||||
while (ptr.next != null) {
|
ptr.next.nodeLock.lock();
|
||||||
ptr.next.lock.lock();
|
var savePtr = ptr;
|
||||||
var saveNode = ptr;
|
ptr = ptr.next;
|
||||||
ptr = ptr.next;
|
savePtr.nodeLock.unlock();
|
||||||
saveNode.lock.unlock();
|
} else
|
||||||
}
|
throw new IndexOutOfBoundsException(index + " out of bounds");
|
||||||
ptr.next = new Node<>(element, ptr, null);
|
|
||||||
} else {
|
}
|
||||||
this.first = new Node<>(element, null, null);
|
ptr.element = element;
|
||||||
this.listLock.unlock();
|
ptr.nodeLock.unlock();
|
||||||
}
|
return element;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the element at the specified position in this list with the
|
* Returns true if this list contains no elements.
|
||||||
* specified element.
|
*
|
||||||
*
|
* @return true if this list contains no elements
|
||||||
* @param index index of the element to replace
|
*/
|
||||||
* @param element element to be stored at the specified position
|
@Override
|
||||||
* @return the element previously at the specified position
|
public boolean isEmpty() {
|
||||||
*/
|
this.listLock.lock();
|
||||||
@Override
|
try { return this.head == null; }
|
||||||
public T set(int index, T element) {
|
finally { this.listLock.unlock();}
|
||||||
this.listLock.lock();
|
}
|
||||||
var ptr = this.first;
|
|
||||||
ptr.lock.lock();
|
|
||||||
this.listLock.unlock();
|
|
||||||
for (var j = 0; j < index; j++) {
|
|
||||||
if (ptr.next != null) {
|
|
||||||
ptr.next.lock.lock();
|
|
||||||
var saveNode = ptr;
|
|
||||||
ptr = ptr.next;
|
|
||||||
saveNode.lock.unlock();
|
|
||||||
} else {
|
|
||||||
throw new IndexOutOfBoundsException(index + " out of bounds");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
var prevElement = ptr.element;
|
|
||||||
ptr.element = element;
|
|
||||||
return prevElement;
|
|
||||||
} finally {
|
|
||||||
ptr.lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if this list contains no elements.
|
|
||||||
*
|
|
||||||
* @return true if this list contains no elements
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
this.listLock.lock();
|
|
||||||
try {
|
|
||||||
return this.first == null;
|
|
||||||
} finally {
|
|
||||||
this.listLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue