Update Java STM API to include STM utils

This commit is contained in:
Peter Vlugter 2011-03-04 10:34:46 +01:00
parent 632848df5a
commit 465e064f79
8 changed files with 253 additions and 0 deletions

View file

@ -0,0 +1,47 @@
package akka.stm.example;
import akka.stm.*;
import akka.actor.*;
public class RetryExample {
public static void main(String[] args) {
System.out.println();
System.out.println("Retry example");
System.out.println();
final Ref<Double> account1 = new Ref<Double>(100.0);
final Ref<Double> account2 = new Ref<Double>(100.0);
ActorRef transferer = Actors.actorOf(Transferer.class).start();
transferer.sendOneWay(new Transfer(account1, account2, 500.0));
// Transferer: not enough money - retrying
new Atomic() {
public Object atomically() {
return account1.set(account1.get() + 2000);
}
}.execute();
// Transferer: transferring
Double acc1 = new Atomic<Double>() {
public Double atomically() {
return account1.get();
}
}.execute();
Double acc2 = new Atomic<Double>() {
public Double atomically() {
return account2.get();
}
}.execute();
System.out.println("Account 1: " + acc1);
// Account 1: 1600.0
System.out.println("Account 2: " + acc2);
// Account 2: 600.0
transferer.stop();
}
}