1.4 dirty read

Dirty read:

For synchronous and asynchronous methods of an object, we design their own program, be sure to consider the overall problem, otherwise it will not match the data errors, classic mistake is to dirty read.

 

 

 

 First, the main thread, a t1 appears walked thread, t1 run method is called setValue (); 1 seconds when the main thread transfer getValue () method;

Viewed from above, within 2 seconds set time t1 worth of threads, the main thread sleep for one second to go after the value of the 

 

 I want to set the value of the time no one to bother me, it is hoped the setValue (), do not go getValue ()

So it is necessary getValue () and setValue () methods are combined with two synchronized keyword to ensure setValue () after the execution, go getValue () value.

===================================================================

Example Summary:

  When we lock on an object's method, we need to consider the overall business, namely setValue / getValue method for simultaneously locking synchronized synchronized keyword to ensure that atomic business (service), otherwise businesses error occurs (also from side to ensure the consistency of business)

 

 incredible!!!

Guess you like

Origin www.cnblogs.com/curedfisher/p/11980674.html