Compare commits

...

4 Commits

Author SHA1 Message Date
Shahnam Javidnia 25297b686f final 2025-05-23 17:52:27 +02:00
Obai Albek 9982245715 fix 2025-05-23 15:36:29 +02:00
Obai Albek 3e6281177a update 2025-05-23 15:12:45 +02:00
Obai Albek f015770a77 Update 2025-05-23 14:52:55 +02:00
2 changed files with 134 additions and 87 deletions

View File

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

View File

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