91 lines
2.9 KiB
Java
91 lines
2.9 KiB
Java
// concurrency/ListComparisons.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// {Args: 1 10 10} (Fast verification check during build)
|
|
// Rough comparison of thread-safe List performance.
|
|
import java.util.concurrent.*;
|
|
import java.util.*;
|
|
import onjava.*;
|
|
|
|
abstract class ListTest extends Tester<List<Integer>> {
|
|
ListTest(String testId, int nReaders, int nWriters) {
|
|
super(testId, nReaders, nWriters);
|
|
}
|
|
class Reader extends TestTask {
|
|
long result = 0;
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < collectionSize; index++)
|
|
result += testCollection.get(index);
|
|
}
|
|
void putResults() {
|
|
readResult += result;
|
|
readTime += duration;
|
|
}
|
|
}
|
|
class Writer extends TestTask {
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < collectionSize; index++)
|
|
testCollection.set(index, writeData[index]);
|
|
}
|
|
void putResults() {
|
|
writeTime += duration;
|
|
}
|
|
}
|
|
void startReadersAndWriters() {
|
|
for(int i = 0; i < nReaders; i++)
|
|
exec.execute(new Reader());
|
|
for(int i = 0; i < nWriters; i++)
|
|
exec.execute(new Writer());
|
|
}
|
|
}
|
|
|
|
class SynchronizedArrayListTest extends ListTest {
|
|
List<Integer> collectionInitializer() {
|
|
return Collections.synchronizedList(
|
|
new ArrayList<>(
|
|
new CountingIntegerList(collectionSize)));
|
|
}
|
|
SynchronizedArrayListTest(int nReaders, int nWriters) {
|
|
super("Synched ArrayList", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
class CopyOnWriteArrayListTest extends ListTest {
|
|
List<Integer> collectionInitializer() {
|
|
return new CopyOnWriteArrayList<>(
|
|
new CountingIntegerList(collectionSize));
|
|
}
|
|
CopyOnWriteArrayListTest(int nReaders, int nWriters) {
|
|
super("CopyOnWriteArrayList", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
public class ListComparisons {
|
|
public static void main(String[] args) {
|
|
Tester.initMain(args);
|
|
new SynchronizedArrayListTest(10, 0);
|
|
new SynchronizedArrayListTest(9, 1);
|
|
new SynchronizedArrayListTest(5, 5);
|
|
new CopyOnWriteArrayListTest(10, 0);
|
|
new CopyOnWriteArrayListTest(9, 1);
|
|
new CopyOnWriteArrayListTest(5, 5);
|
|
Tester.exec.shutdown();
|
|
}
|
|
}
|
|
/* Output:
|
|
Type Read time Write time
|
|
Synched ArrayList 10r 0w 700989 0
|
|
Synched ArrayList 9r 1w 2531313 60622
|
|
readTime + writeTime = 2591935
|
|
Synched ArrayList 5r 5w 1396291 649761
|
|
readTime + writeTime = 2046052
|
|
CopyOnWriteArrayList 10r 0w 551005 0
|
|
CopyOnWriteArrayList 9r 1w 161945 123521
|
|
readTime + writeTime = 285466
|
|
CopyOnWriteArrayList 5r 5w 549580 4230146
|
|
readTime + writeTime = 4779726
|
|
*/
|