[Practical Java high concurrency Program Design 3] References to objects with timestamps: AtomicStampedReference, java high concurrency Program Design
[Practice: Java high concurrency Program Design 1] pointer in Java: Unsafe class
[Java high concurrency program design 2] Lock-free Object Reference: AtomicReference
The root cause of AtomicReference's failure to solve the above problem is that the object's status information is lost during modification. The object value and status are marked with equal signs. Therefore, as long as we can record the state value of the object during the modification process, we can solve the problem that the thread cannot correctly judge the object state due to repeated object modifications.
This is exactly what AtomicStampedReference does. It not only maintains the object value, but also maintains a timestamp (I call it a timestamp here. In fact, it can make any integer, which uses an integer to represent the State value ). When the value corresponding to AtomicStampedReference is modified, the timestamp must be updated in addition to the update data. When AtomicStampedReference sets the object value, both the object Value and the timestamp must meet the expected value. Therefore, even if the object value is read and written repeatedly and written back to the original value, as long as the timestamp changes, it can prevent improper writing.
Several APIs of AtomicStampedReference add timestamp Information Based on AtomicReference:
// Compare setting parameters: Expected Value written to new value expected timestamp new timestamp public boolean compareAndSet (V expectedReference, V newReference, int expectedStamp, int newStamp) // obtain the current object reference public V getReference () // obtain the current timestamp public int getStamp () // set the current object reference and timestamp public void set (V newReference, int newStamp)
With AtomicStampedReference, we don't have to worry about object writing! Now, let's use AtomicStampedReference to fix the VIP card recharge problem:
01 public class AtomicStampedReferenceDemo {02 static AtomicStampedReference <Integer> money = new AtomicStampedReference <Integer> (19,0); 03 public staticvoid main (String [] args) {04 // simulate multiple threads to update the background database at the same time and recharge the user 05 for (int I = 0; I <3; I ++) {06 final int timestamp = money. getStamp (); 07 newThread () {08 public void run () {09 while (true) {10 while (true) {11 Integerm = money. getReference (); 12 if (m <20) {13 if (money. compareAndSet (m, m + 20, timestamp, timestamp + 1) {14 System. out. println ("the balance is less than 20 yuan, the recharge is successful, the balance is:" + money. getReference () + ""); 15 break; 16} 17} else {18 // System. out. println ("balance greater than 20 yuan, no need to recharge"); 19 break; 20} 21} 22} 23} 24 }. start (); 25} 26 27 // user consumption Thread, simulating consumption behavior 28 new Thread () {29 publicvoid run () {30 for (int I = 0; I <100; I ++) {31 while (true) {32 int timestamp = money. getStamp (); 33 Integer m = money. getReference (); 34 if (m> 10) {35 System. out. println ("more than 10 yuan"); 36 if (money. compareAndSet (m, m-10, timestamp, timestamp + 1) {37 System. out. println ("successfully consumed 10 yuan, balance:" + money. getReference (); 38 break; 39} 40} else {41 System. out. println ("not enough"); 42 break; 43} 44} 45 try {Thread. sleep (100);} catch (InterruptedException e) {}46} 47} 48 }. start (); 49} 50}
Row 3: We use AtomicStampedReference to replace the original AtomicReference. Row 3 obtains the account timestamp. Subsequent gift operations are based on this timestamp. If it is successfully granted (13 rows), The timestamp is modified. This makes it impossible for the system to have a secondary gift. The consumption thread is similar. Each operation adds 1 (36 rows) to the timestamp to make it impossible to repeat.
Run the above code to get the following output:
If the balance is less than 20 yuan, recharge is successful. Balance: 39 yuan is greater than 10 yuan, and 10 yuan is successfully consumed. Balance: 29 is greater than 10 yuan, and 10 yuan is successfully consumed. Balance: 19 if you have successfully consumed 10 RMB for a fee of more than 10 RMB, you can see that there is not enough balance in the balance: 9. The account is only given once.
[Practice: Java high concurrency Program Design 1] pointer in Java: Unsafe class
[Java high concurrency program design 2] Lock-free Object Reference: AtomicReference
Excerpt: Java high concurrency Program Design