Multithreading learn as much visibility thread

Visibility multithreading

  First, delve into uncertainties multithreading

. 1 . 1 , cpu obtain a very short time due to cache data than the desired
 2 2 itself after the optimization, cpu command causes data rearrangement cause confusion; of course, in the present case the value based on the multi-threaded, single thread this problem will not arise.
. 3 . 3, the JIT method will repeatedly call a multi-zone or multiple cycles of data storage change, then the self-optimization; this will lead to the data cache can not be changed later normally acquired.

  First of all cpu cache, it is between us and the physical memory program.

  CPU cache (English: CPU Cache, herein referred to as buffer) for reducing the processor to access the memory means to the desired average time. In pyramid storage system which is located in the second layer of top-down, after the CPU registers.

  Which is much smaller than the capacity of memory , but the speed may be close to the frequency of the processor.

  When the processor issues a memory access request, the request will first see if there is data in the cache. If there (a hit), not directly access the memory through the data return; if not (fail), will have first loaded the corresponding data in the cache memory, the processor then returns.
 
  Cache valid reason, mainly because the program is running access to memory presents locality (Locality) feature. This includes both local spatial locality (Spatial Locality), including temporal locality (Temporal Locality).
 
  Effective use of this locality, the cache can achieve very high hit rate.
 
  It appears in the processor, the cache is a transparent member. Therefore, the programmer usually can not directly intervene in the operation of the cache. However, indeed program code optimized for the specific embodiment the characteristics of the buffer, so as to better utilize the cache.
  
  Well respective data loading buffer memory here, then the processor returns. In multithreaded scenarios, it will lead to not result in data acquisition in a very short period of time.
 

  CPU instruction rearrangement: this case does not matter how rearrangement in a single thread, the value will not be confused, but in a multithreaded environment may occur value confusion.

1  // That Java compiler may rearrange the order of the source code that is executed by the compiler optimization performance, such as 
2  
. 3  int A = 0 , 
 . 4  int B = A + 1 ;
 . 5  int C = 2 ;
 . 6  
. 7  // compiled after into bytecode, the order may be executed 
. 8  int C = 2 ;
 . 9  int B = a +. 1 ;
 10  int a = 0;

  JIT execution model:

 

 

   Two, JMM memory model

    Java memory model provides:
    > Read write operations to a volatile happens-before each subsequent field in the volatile field.
    > Written to the volatile variable v, v synchronous read and follow all other threads;
    
    How Volatile achieve its semantics:
    > Disable caching;
    volatile variable access control characters will add ACC_VOLATILE
    https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
    > Instruction volatile variable reordering is not relevant;
 
    In order to ensure that multi-threaded visibility.

Guess you like

Origin www.cnblogs.com/zhanvo/p/11845693.html