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

View File

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