45
|
1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction;
|
|
2
|
|
3 import java.util.concurrent.atomic.AtomicBoolean;
|
|
4 import java.util.concurrent.atomic.AtomicReference;
|
|
5
|
|
6 public class AtomicReservableReference<V>
|
|
7 {
|
|
8 public static void main(String _args[])
|
|
9 {
|
|
10 String hoge = "hoge";
|
|
11 String fuga = "fuga";
|
|
12 AtomicReservableReference<String> arr = new AtomicReservableReference<String>(hoge);
|
|
13 AtomicReservableReference<String>.Reservation r1 = arr.makeReservation(hoge,fuga);
|
81
|
14 //AtomicReservableReference<String>.Reservation r2 = arr.makeReservation(hoge,fuga);
|
45
|
15
|
|
16 System.out.println(arr.get());
|
|
17 r1.confirm();
|
|
18 System.out.println(arr.get());
|
|
19 }
|
|
20
|
|
21 private AtomicReference<Reservation> reservation;
|
|
22 private AtomicBoolean flagReservation;
|
|
23
|
|
24 public AtomicReservableReference(V _ref)
|
|
25 {
|
|
26 reservation = new AtomicReference<Reservation>(new Reservation(_ref,null,true));
|
|
27 flagReservation = new AtomicBoolean(false);
|
|
28 }
|
|
29
|
|
30 public Reservation makeReservation(V _expect,V _update)
|
|
31 {
|
|
32 if(flagReservation.compareAndSet(false,true)){
|
|
33 V value = get();
|
|
34 if(value == _expect){
|
|
35 Reservation expect = reservation.get();
|
|
36 return new Reservation(_update,expect,false);
|
|
37 }
|
|
38 flagReservation.set(false);
|
|
39 }
|
|
40
|
|
41 return null;
|
|
42 }
|
|
43
|
|
44 public void set(V _value)
|
|
45 {
|
|
46 reservation.set(new Reservation(_value,null,true));
|
|
47 }
|
|
48
|
|
49 public V get()
|
|
50 {
|
|
51 Reservation r = reservation.get();
|
|
52 return r.get();
|
|
53 }
|
|
54
|
|
55 public class Reservation
|
|
56 {
|
|
57 public final Reservation expect;
|
|
58 public final AtomicReference<V> ref;
|
|
59 public final AtomicBoolean flagConfirmed;
|
|
60
|
|
61 public Reservation(V _object,Reservation _expect,boolean _confirmed)
|
|
62 {
|
|
63 expect = _expect;
|
|
64 ref = new AtomicReference<V>(_object);
|
|
65 flagConfirmed = new AtomicBoolean(_confirmed);
|
|
66 }
|
|
67
|
|
68 public V get()
|
|
69 {
|
|
70 return ref.get();
|
|
71 }
|
|
72
|
|
73 public void confirm()
|
|
74 {
|
|
75 if(flagConfirmed.compareAndSet(false,true)){
|
|
76 if(reservation.compareAndSet(expect,this) == false){
|
|
77 throw new IllegalStateException("foo!");
|
|
78 }
|
|
79 flagReservation.set(false);
|
|
80 }
|
|
81 }
|
|
82
|
|
83 public void cancel()
|
|
84 {
|
|
85 if(flagConfirmed.compareAndSet(false,true)){
|
|
86 flagReservation.set(true);
|
|
87 }
|
|
88 }
|
|
89 }
|
|
90 }
|