-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestForMultiverse.java
55 lines (44 loc) · 1.38 KB
/
TestForMultiverse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.multiverse.test;
import static org.multiverse.api.StmUtils.newTxnLong;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.multiverse.api.StmUtils;
import org.multiverse.api.references.TxnLong;
class Account{
final TxnLong amount;
public Account(long amount){
this.amount = newTxnLong(amount);
}
public static void transfer(final Account from, final Account to, final long amount){
StmUtils.atomic(new Runnable(){
public void run(){
from.amount.decrement(amount);
to.amount.increment(amount);
}
});
}
}
public class TestForMultiverse {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
final Account account1 = new Account(100);
final Account account2 = new Account(100);
ExecutorService executor = Executors.newCachedThreadPool();
for( int i = 0; i <2; ++i)
executor.execute(new Runnable(){
public void run(){
Account.transfer(account1, account2, 1);
}
});
executor.shutdown();
executor.awaitTermination(1L, TimeUnit.HOURS);
System.out.println();
StmUtils.atomic(new Runnable(){
public void run(){
System.out.println(account1.amount.get());
System.out.println(account2.amount.get());
}
});
}
}