2010-10-26 12:49:25 +02:00
|
|
|
package akka.stm;
|
2010-08-16 11:38:39 +12:00
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
import akka.stm.Ref;
|
|
|
|
|
import akka.stm.local.Atomic;
|
2010-08-16 11:38:39 +12:00
|
|
|
|
|
|
|
|
public class CounterExample {
|
|
|
|
|
final static Ref<Integer> ref = new Ref<Integer>(0);
|
|
|
|
|
|
|
|
|
|
public static int counter() {
|
|
|
|
|
return new Atomic<Integer>() {
|
|
|
|
|
public Integer atomically() {
|
|
|
|
|
int inc = ref.get() + 1;
|
|
|
|
|
ref.set(inc);
|
|
|
|
|
return inc;
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("Counter example");
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("counter 1: " + counter());
|
|
|
|
|
System.out.println("counter 2: " + counter());
|
|
|
|
|
}
|
|
|
|
|
}
|