SL_U2_SJ
Obai Albek 2025-05-23 15:12:45 +02:00
parent f015770a77
commit 3e6281177a
1 changed files with 3 additions and 9 deletions

View File

@ -3,7 +3,6 @@ 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 lombok.Synchronized;
import net.jcip.annotations.ThreadSafe; import net.jcip.annotations.ThreadSafe;
@ThreadSafe @ThreadSafe
@ -37,7 +36,6 @@ 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
@Synchronized
public T get(int index) { public T get(int index) {
this.listLock.lock(); this.listLock.lock();
var ptr = this.first; var ptr = this.first;
@ -50,9 +48,8 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
ptr = ptr.next; ptr = ptr.next;
savePtr.lock.unlock(); savePtr.lock.unlock();
} else { } else {
return null; throw new IndexOutOfBoundsException(index + " out of bounds");
} }
ptr = ptr.next;
} }
try { try {
return (ptr.element); return (ptr.element);
@ -72,7 +69,6 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* *
*/ */
@Override @Override
@Synchronized
public boolean add(T element) { public boolean add(T element) {
this.listLock.lock(); this.listLock.lock();
if (this.first != null) { if (this.first != null) {
@ -102,12 +98,11 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @return the element previously at the specified position * @return the element previously at the specified position
*/ */
@Override @Override
@Synchronized
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.first;
ptr.lock.lock(); ptr.lock.lock();
this.listLock.lock(); this.listLock.unlock();
for (var j = 0; j < index; j++) { for (var j = 0; j < index; j++) {
if (ptr.next != null) { if (ptr.next != null) {
ptr.next.lock.lock(); ptr.next.lock.lock();
@ -115,7 +110,7 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
ptr = ptr.next; ptr = ptr.next;
saveNode.lock.unlock(); saveNode.lock.unlock();
} else { } else {
return null; throw new IndexOutOfBoundsException(index + " out of bounds");
} }
} }
var prevElement = ptr.element; var prevElement = ptr.element;
@ -130,7 +125,6 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
* @return true if this list contains no elements * @return true if this list contains no elements
*/ */
@Override @Override
@Synchronized
public boolean isEmpty() { public boolean isEmpty() {
this.listLock.lock(); this.listLock.lock();
try { try {