Multithreading -volatile keywords and ThreadLocal Detailed volatile keyword and ThreadLocal

volatile keyword and ThreadLocal

1, the three concurrent programming concepts

Atomicity: one or more operations. Either all execution and completion of the implementation process will not be interrupted or not performed. The most common example: i ++ / i-- operation. Not atomic operations, if you do not do thread synchronization likely to cause safety problems.

Visibility: multiple threads access the same variable, a thread changed the value of this variable, other threads can see the value changes immediately. The visibility of the issue, there are two ways to guarantee. First, the volatile keyword, the second is through synchronized and lock. Details later.

Ordering: the program execution sequence performed in the order code.

To understand the need to know about ordering instruction reordering. In order to provide the operating efficiency of the processor, code optimization will not guarantee the order of execution of each statement, but will ensure the implementation of the results of the implementation of the code is consistent with the order, it does not affect the results of a single thread, but will affect the accuracy of concurrently executing threads. Instruction reordering considers data dependencies between instructions, must be used if a command B A result of the instruction, the processor will be performed to ensure that prior to A B.

To ensure the correct implementation of concurrent programs, we must ensure atomicity, visibility and orderliness. As long as there is not a guarantee, it could cause the program to run incorrectly.

 2, Java Memory Model

Java memory model specifies: the presence of main memory all variables, each thread has its own working memory. Threaded operation of the variable must be in working memory, main memory and can not operate directly. And each thread can not access the working memory of other threads.

Atomicity, visibility and ensure orderliness of JAVA language itself provides:

Atomicity : java, for the reference variable, and most of the read original data type (except for long and double) operations are atomic. These operations can not be interrupted, either executed or not executed. For all the read and write variables declared as volatile, they are atoms (except for long and double)

Visibility : java provides the volatile keyword to ensure visibility.

Ordering : java memory model, it allows the compiler and processor instructions to be reordered. It will affect the accuracy of concurrent execution of multiple threads. In the java can be ensured by ordering the volatile keyword, as well as synchronized and lock. synchronized and lock to ensure that each time only one thread synchronization code execution, making thread serialization perform synchronization code, to ensure the orderly. How volatile guaranteed explain later.

 3, volatile keyword Detailed

A shared variable (class member variables, static class member variable) modified after being volatile, it has the two semantics: to ensure the visibility and prohibits the reordering command when a different thread on the operating variable.

java provides the volatile keyword to ensure visibility:

When a shared variable is declared volatile, it will ensure that the modified values are immediately updated to the main memory. The new value will be read from memory while other threads read. Ordinary share variables can not guarantee the visibility, it is written to memory timing uncertain. When read other threads may read the old value. 
Further synchronized by lock and visibility can be secured. They can ensure that only one thread acquires the lock and then synchronize code. And before the release of the lock modify variables flushed to live in memory. In order to ensure visibility.
principle and mechanism of the volatile keyword: 
When adding the volatile keyword, will be more of a lock prefix instructions. lock prefix instructions corresponding to a memory barrier that provides three functions. 
  1, it will be forced to modify the cache operation immediately written to main memory. 
  2, if the write operation, it causes other invalid cache line corresponding to the CPU 
  3, it will not ensure that its subsequent instructions discharged position before the memory barrier reordering specified, nor will the preceding instruction is discharged memory behind the barrier. I.e., when executing the memory barrier command phrase, operating in front of it has been completed.

Note that: volatile can not guarantee that any operations on variables are atomic.

You must have two conditions for the use of the volatile keyword:

  1, write to the variable does not depend on the current value.

  2, the variable is not included in the invariant with other variables.

4, ThreadLocal Detailed

ThreadLocal, thread-local variables can also be called thread local storage. It maintains a separate copy of the variable for each thread. The visible range is limited to objects within the same thread.

ThreadLoca class provides several common methods:

public T get () {} --- acquired ThreadLocal saved copy of the variable in the current thread 
public void set (T value) { } --- copy of the current thread is provided in the variable 
public void remove () {} --- shift in addition to the variables in the copy of the current thread 
protected T initialValue () {} --- protected modified method. ThreadLocal provides only a shallow copy, if the variable is a reference type, then we must rewrite this function to achieve a deep copy. It is recommended when using ThreadLocal start rewriting the function

ThreadLocal action: achieving local variable within the scope of the thread, i.e. a thread in ThreadLocal is shared between different threads are isolated.

ThreadLocal principle: use ThreadLocal stored value of the current ThreadLocal instance as key, stored in the current thread object Map go. Before beginning to see the source code, I think that is the current thread object as the key will be deposited in the ThreadLocal object to the Map.

Guess you like

Origin www.cnblogs.com/alex-xyl/p/12462381.html