Atomic integer demonstration
For those who are curious...
Here is a class that compares the behavior of a unary ++ operation on a primitive integer and the corresponding method in java.util.concurrent.atomic.AtomicIntege r when they are accessed by concurrent threads.
java -classpath atom.jar Atom 10 1000
This will start 10 threads, let them run for approximately 1000 milliseconds and print the shared primitive and atomic counters, the sum of individual counters for all threads, and the differences.
Requires Java 5.
On a 2GHz Windows 2000 workstation, the primitive integer is off about half of the time, and on a multiprocessor Linux server, every time I tried, even with 2 threads and 100 ms.
import java.util.concurrent.atomic.*;
public class Atom {
private static int x = 0;
private static AtomicInteger X = new AtomicInteger();
private static boolean runFlag = true;
public static class Adder extends Thread {
private int y = 0;
private boolean isRunning;
public Adder(String name) {
super(name);
}
public void run() {
while (runFlag) {
x++;
y++;
X.incrementAndGet();
}
}
}
public static void main(String[] args) throws InterruptedException {
if (args.length!=2) {
System.out.println("arguments: threads duration(ms)");
return;
}
int threads = Integer.parseInt(args[0]);
long duration = Long.parseLong(args[1]);
Adder[] adders = new Adder[threads];
for (int i = 0; i < threads; i++) {
Adder a = new Adder("adder-"+i);
adders[i] = a;
a.start();
}
Thread.sleep(duration);
runFlag = false;
int sumY = 0;
for (Adder a : adders) {
/*
while (a.isAlive()) {
Thread.sleep(10);
}
*/
a.join();
sumY += a.y;
}
System.out.println("Primitive integer shared counter...");
System.out.println("x = " + x);
System.out.println("Atomic integer shared counter...");
System.out.println("X = " + X);
System.out.println();
System.out.println("Sum of individual counters...");
System.out.println("sum(y) = " + sumY);
System.out.println();
System.out.println("Differences...");
System.out.println("x - sum(y) = " + (x - sumY));
System.out.println("X - sum(y) = " + (X.intValue() - sumY));
}
}
Here is a class that compares the behavior of a unary ++ operation on a primitive integer and the corresponding method in java.util.concurrent.atomic.AtomicIntege
java -classpath atom.jar Atom 10 1000
This will start 10 threads, let them run for approximately 1000 milliseconds and print the shared primitive and atomic counters, the sum of individual counters for all threads, and the differences.
Requires Java 5.
On a 2GHz Windows 2000 workstation, the primitive integer is off about half of the time, and on a multiprocessor Linux server, every time I tried, even with 2 threads and 100 ms.
import java.util.concurrent.atomic.*;
public class Atom {
private static int x = 0;
private static AtomicInteger X = new AtomicInteger();
private static boolean runFlag = true;
public static class Adder extends Thread {
private int y = 0;
private boolean isRunning;
public Adder(String name) {
super(name);
}
public void run() {
while (runFlag) {
x++;
y++;
X.incrementAndGet();
}
}
}
public static void main(String[] args) throws InterruptedException {
if (args.length!=2) {
System.out.println("arguments: threads duration(ms)");
return;
}
int threads = Integer.parseInt(args[0]);
long duration = Long.parseLong(args[1]);
Adder[] adders = new Adder[threads];
for (int i = 0; i < threads; i++) {
Adder a = new Adder("adder-"+i);
adders[i] = a;
a.start();
}
Thread.sleep(duration);
runFlag = false;
int sumY = 0;
for (Adder a : adders) {
/*
while (a.isAlive()) {
Thread.sleep(10);
}
*/
a.join();
sumY += a.y;
}
System.out.println("Primitive integer shared counter...");
System.out.println("x = " + x);
System.out.println("Atomic integer shared counter...");
System.out.println("X = " + X);
System.out.println();
System.out.println("Sum of individual counters...");
System.out.println("sum(y) = " + sumY);
System.out.println();
System.out.println("Differences...");
System.out.println("x - sum(y) = " + (x - sumY));
System.out.println("X - sum(y) = " + (X.intValue() - sumY));
}
}
