The first part of JAVA multithreading (a) thread safety foundation

Concurrent Notes Portal:
1.0 Concurrent Programming-Mind Map
2.0 Concurrent Programming-Thread Safety Fundamentals
3.0 Concurrent Programming-Basic Building Module
4.0 Concurrent Programming-Task Execution-Future
5.0 Concurrent Programming-Multi-threaded Performance and Scalability
6.0 Concurrent Programming- Explicit lock and synchronized
7.0 concurrent programming-AbstractQueuedSynchronizer
8.0 concurrent programming-atomic variables and non-blocking synchronization mechanism

Minimum security

When a thread reads a variable without synchronization, it may get an invalid value, but at least this value is a value set by a previous thread, not a random value. This kind of security guarantee is called minimum security.

The minimum security applies to most variables, but there is one exception, non-volatile 64-bit numeric variables (double and lang). The Java memory model requires that variable read and write operations must be atomic operations, but for non-volatile long and double variables, the JVM allows the 64-bit read or write operation to be decomposed into two 32-bit operations.

Volatile variables

A weaker synchronization mechanism to ensure that other threads are notified of variable update operations. When declared as a volatile type, both the compiler and runtime will notice that this variable is shared, so operations on this variable will not be reordered with other memory operations. Volatile variables will not be cached in registers or other places that are not visible to the processor, so when reading a volatile variable, the latest written value will always be returned.

The locking mechanism can ensure both visibility and atomicity, while volatile variables can only ensure visibility.

  • volatile use conditions (if and only if)
    • The write operation to the variable does not depend on the current value of the variable, or to ensure that only a single thread updates the variable value.
    • This variable will not be included in the invariance condition along with other state variables.
    • No lock is required when accessing variables.

Thread closure

Avoid using synchronization and access data in a single thread. Reliable thread closure technology includes 栈封闭andThreadLocal

Ad-hoc thread closure

维护线程封闭性的职责完全由程序实现来承担。该方式是非常脆弱的。
可以通过在 volatile 变量上实现一种特殊的线程封闭。
确保只有单个线程对共享的 volatile 变量执行写入操作,
那么就可以安全的就可以安全的在这些共享的 volatile 变量上执行·读取·-·修改·-·写入·的操作。
在这种情况下,相当于将修改操作封闭在单个线程中以防止发生竟态条件,并且 volatile 变量还确保了其可见性。

Stack closed

线程封闭的一种特例,栈封闭中,只能通过局部变量才能访问对象。
局部变量的固有属性之一就是封闭在执行线程中。
它们位于执行线程的栈中,其他线程无法访问这个栈。

ThreadLocal 类

Click here for detailed source code

ThreadLocal 提供了 get 和 set 等访问接口和方法。
这些方法为每个使用该变量的线程都存有一份独立的副本,
因此 get 总是返回由当前执行线程在调用 set 时设置的最新值。

Immutability

Immutable object

After an object is created, its state cannot be modified, then this object is called an immutable object.

不可变对象很简单,它们只有一种状态,并且该状态由构造函数控制。

Final domain

final is used to construct immutable objects. The final field is not modifiable (but if the object referenced by the final field is mutable, then these referenced objects can be modified)
Deep understanding of the final keyword

final 域能确保初始化过程的安全性,从而可以不受限制的访问不可变对象,并在共享这些对象时无需同步。

Security release object

要安全发布一个对象,对象的引用和状态必须同时对其他线程可见。
一个正确构造的对象可以通过以下方式来安全发布。
    * 在静态初始化函数中初始化一个对象引用
    * 将对象的引用保存到 volatile 类型的域或AtomicReferance中
    * 将对象的引用保存到某个正确构造对象的 final 域中
    * 将对象的引用保存到由锁保护的域中

Guess you like

Origin blog.csdn.net/lijie2664989/article/details/103234782