Skip to content

Instantly share code, notes, and snippets.

@ankurpshah
Created July 27, 2013 10:11
Show Gist options
  • Save ankurpshah/6094462 to your computer and use it in GitHub Desktop.
Save ankurpshah/6094462 to your computer and use it in GitHub Desktop.
STM Example
package stm;
import akka.stm.*;
public class AkkaSTMIntegerCounter {
private final Ref<Integer> ref = new Ref<Integer>(0);
public 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) {
AkkaSTMIntegerCounter counterRef = new AkkaSTMIntegerCounter();
System.out.println(counterRef.counter());
// -> 1
System.out.println(counterRef.counter());
// -> 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment