- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestForMultiverse.java
More file actions
Latest commit
55 lines (44 loc) · 1.38 KB
/
Copy pathTestForMultiverse.java
File metadata and controls
55 lines (44 loc) · 1.38 KB
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
packagecom.multiverse.test;
importstaticorg.multiverse.api.StmUtils.newTxnLong;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.TimeUnit;
importorg.multiverse.api.StmUtils;
importorg.multiverse.api.references.TxnLong;
classAccount{
finalTxnLongamount;
publicAccount(longamount){
this.amount = newTxnLong(amount);
}
publicstaticvoidtransfer(finalAccountfrom, finalAccountto, finallongamount){
StmUtils.atomic(newRunnable(){
publicvoidrun(){
from.amount.decrement(amount);
to.amount.increment(amount);
}
});
}
}
publicclassTestForMultiverse {
publicstaticvoidmain(String[] args) throwsInterruptedException {
// TODO Auto-generated method stub
finalAccountaccount1 = newAccount(100);
finalAccountaccount2 = newAccount(100);
ExecutorServiceexecutor = Executors.newCachedThreadPool();
for( inti = 0; i <2; ++i)
executor.execute(newRunnable(){
publicvoidrun(){
Account.transfer(account1, account2, 1);
}
});
executor.shutdown();
executor.awaitTermination(1L, TimeUnit.HOURS);
System.out.println();
StmUtils.atomic(newRunnable(){
publicvoidrun(){
System.out.println(account1.amount.get());
System.out.println(account2.amount.get());
}
});
}
}