Created
July 27, 2013 10:11
-
-
Save ankurpshah/6094462 to your computer and use it in GitHub Desktop.
STM Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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