Compare commits
3 Commits
c9b1dfbe48
...
bde868ad47
Author | SHA1 | Date |
---|---|---|
|
bde868ad47 | |
|
7093dea6fa | |
|
d5e78f1dfe |
|
@ -2,6 +2,7 @@ package pp;
|
||||||
|
|
||||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
private Node<T> first;
|
private Node<T> first;
|
||||||
|
private final Object headLock = new Object();
|
||||||
|
|
||||||
private static class Node<U> {
|
private static class Node<U> {
|
||||||
U element;
|
U element;
|
||||||
|
@ -22,36 +23,33 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(T element) {
|
public boolean add(T element) {
|
||||||
synchronized (this) {
|
Node<T> curr;
|
||||||
|
|
||||||
|
synchronized (headLock) {
|
||||||
if (first == null) {
|
if (first == null) {
|
||||||
first = new Node<>(element, null, null);
|
first = new Node<>(element, null, null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
curr = first;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node<T> curr;
|
while (true) {
|
||||||
synchronized (first.lock) {
|
Node<T> next;
|
||||||
curr = first;
|
synchronized (curr.lock) {
|
||||||
while (true) {
|
next = curr.next;
|
||||||
Node<T> next;
|
if (next == null) {
|
||||||
synchronized (curr.lock) {
|
curr.next = new Node<>(element, curr, null);
|
||||||
next = curr.next;
|
return true;
|
||||||
if (next == null) {
|
|
||||||
curr.next = new Node<>(element, curr, null);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
synchronized (next.lock) {
|
|
||||||
curr = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
curr = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
Node<T> curr;
|
Node<T> curr;
|
||||||
synchronized (this) {
|
synchronized (headLock) {
|
||||||
if (first == null) throw new IndexOutOfBoundsException();
|
if (first == null) throw new IndexOutOfBoundsException();
|
||||||
curr = first;
|
curr = first;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +69,7 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
@Override
|
@Override
|
||||||
public T set(int index, T element) {
|
public T set(int index, T element) {
|
||||||
Node<T> curr;
|
Node<T> curr;
|
||||||
synchronized (this) {
|
synchronized (headLock) {
|
||||||
if (first == null) throw new IndexOutOfBoundsException();
|
if (first == null) throw new IndexOutOfBoundsException();
|
||||||
curr = first;
|
curr = first;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +90,7 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
synchronized (this) {
|
synchronized (headLock) {
|
||||||
return first == null;
|
return first == null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue