@@ -0,0 +1,1808 @@
+package org.isis.speedtests;%0A%0Aimport java.util.concurrent.atomic.*;%0A%0Apublic class AtomicTest %7B%0A%0A%09interface Test %7B%0A%09%09void increment();%0A%0A%09%09int get();%0A%09%7D%0A%0A%09static class Test0 implements Test %7B%0A%09%09int counter = 0;%0A%0A%09%09public void increment() %7B%0A%09%09%09++counter;%0A%09%09%7D%0A%0A%09%09public int get() %7B%0A%09%09%09return counter;%0A%09%09%7D%0A%09%7D;%0A%0A%09static class Test1 implements Test %7B%0A%09%09AtomicInteger counter = new AtomicInteger();%0A%0A%09%09public void increment() %7B%0A%09%09%09counter.incrementAndGet();%0A%09%09%7D%0A%0A%09%09public int get() %7B%0A%09%09%09return counter.get();%0A%09%09%7D%0A%09%7D;%0A%0A%09static class Test2 implements Test %7B%0A%09%09int counter = 0;%0A%0A%09%09public synchronized void increment() %7B%0A%09%09%09++counter;%0A%09%09%7D%0A%0A%09%09public synchronized int get() %7B%0A%09%09%09return counter;%0A%09%09%7D%0A%09%7D;%0A%0A%09static class Test3 implements Test %7B%0A%09%09volatile int counter = 0;%0A%0A%09%09static AtomicIntegerFieldUpdater%3CTest3%3E updater = AtomicIntegerFieldUpdater%0A%09%09%09%09.newUpdater(Test3.class, %22counter%22);%0A%0A%09%09public void increment() %7B%0A%09%09%09updater.incrementAndGet(this);%0A%09%09%7D%0A%0A%09%09public int get() %7B%0A%09%09%09return updater.get(this);%0A%09%09%7D%0A%09%7D;%0A%0A%09static class Worker extends Thread %7B%0A%09%09Test test;%0A%0A%09%09Worker(Test test) %7B%0A%09%09%09this.test = test;%0A%09%09%7D%0A%0A%09%09public void run() %7B%0A%09%09%09for (int i = 0; i %3C 5000000; ++i)%0A%09%09%09%09test.increment();%0A%09%09%7D%0A%09%7D%0A%0A%09static void execute(Test test) throws Exception %7B%0A%09%09long time = System.currentTimeMillis();%0A%0A%09%09Thread threads%5B%5D = new Thread%5B20%5D;%0A%0A%09%09for (int i = 0; i %3C threads.length; ++i)%0A%09%09%09threads%5Bi%5D = new Worker(test);%0A%0A%09%09for (int i = 0; i %3C threads.length; ++i)%0A%09%09%09threads%5Bi%5D.start();%0A%0A%09%09for (int i = 0; i %3C threads.length; ++i)%0A%09%09%09threads%5Bi%5D.join();%0A%0A%09%09time = System.currentTimeMillis() - time;%0A%09%09System.out.println(%22time %22 + time + %22 counter %22 + test.get());%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) throws Exception %7B%0A%09%09execute(new Test0());%0A%09%09execute(new Test1());%0A%09%09execute(new Test2());%0A%09%09execute(new Test3());%0A%09%7D%0A%7D%0A

test the usability of atomic objects