Study notes (34): Chapter 1 Distributed Foundation of Concurrent Programming-Challenges of Concurrent Programming 01

Learn now: https://edu.csdn.net/course/play/29000/405248?utm_source=blogtoedu

1. Atomicity: At the thread level, it is impossible for a series of instructions to intervene. Once started, it cannot be interrupted by other threads or CPU

1.1, the common verification method of atomicity count++;

This instruction can actually be broken down into four-step assembly instructions

(1) getstatic accesses a static variable to obtain the original value of count, for example, 3

(2) iconst_1 constant 1 is pushed onto the stack

(3) iadd increases by 3+1 to 4

(4) putstatic sets the static variable count to point to 4 and writes to the main memory

These four steps can only be uninterrupted or divided, which is called atomicity. If one of the stages is interrupted or switched by the CPU in turn, it will cause count++ a thousand times and the final result is not 1000

2. Visibility

3、Synchronized

(1) Put on the method in the class

synchronized void demo(){} 相当于 synchronized(this){}

(2) Put in the code block

void demo(){synchronized(obj){}}

4. Scope of syncheonized lock

(1) Instance lock

A a=new A();

A b=new A();

Two threads access a, then the lock is mutually exclusive at this time, because it is the same instance a

Two threads access a and b respectively, then the lock is not mutually exclusive at this time, because it is not the same instance

(2) Object lock: reflected in static method or class object, class lock

Static method: There is a static method in A. Static method = "synchronized static void demo(); equivalent to class lock = "void demo() {synchronized(A.class){}}

Two threads respectively access a.demo and b.demo. Although there are two instances a and b, they are mutually exclusive at this time, because synchronized modifies static methods or modifies class locks

(3) The code block reflects the scope of mutual exclusion

5. The essence of mutual exclusion locks: sharing resources

6. Lock storage (object header)

In an object of new, there will be a part of space to store the lock state and mark

6.1. The layout of a lock object in memory is:

(1) Object header: contains object mark and class meta information

(2) Example data 

7. Lock status changes = "Locking will definitely bring performance overhead

After JDK1.6, the lock state has been optimized and changed to no lock state, lightweight lock, heavyweight lock, GC mark, and biased lock.

 

 

 

Guess you like

Origin blog.csdn.net/qq_28500837/article/details/112883924