SL_U2_SJ
Obai Albek 2025-05-23 15:36:29 +02:00
parent 3e6281177a
commit 9982245715
2 changed files with 28 additions and 26 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

@ -52,9 +52,8 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
}
}
try {
return (ptr.element);
}
finally {
return delay(ptr.element);
} finally {
ptr.lock.unlock();
}
}
@ -113,10 +112,13 @@ public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
throw new IndexOutOfBoundsException(index + " out of bounds");
}
}
var prevElement = ptr.element;
ptr.element = element;
ptr.lock.unlock();
return prevElement;
try {
var prevElement = ptr.element;
ptr.element = element;
return prevElement;
} finally {
ptr.lock.unlock();
}
}
/**