1. Basic Concepts
Program: Static, the code used to complete certain functions.
Process: dynamic, running program
Thread: The actual operation of the unit process, a process can contain one or more threads.
2. JVM memory area
- Heap: threads share, store instance object (OOM)
- Virtual machine stack: the private address of the thread, Java methods at runtime memory model (OOM), the local variable storage, data reference type, the operand stack
- Native method stacks
- Methods District: threads share, store class information, constants, static variables, etc.
- Program Counter: thread private, storage address of the next instruction
3. java memory model (java memory model, JMM, abstract models)
Role: interactive specification memory space and work space data
Main memory: information sharing by threads
Working memory: thread private information. Basic data types, assigned directly to the working memory. Address reference stored in working memory, object references are stored in a heap.
Way of working:
Thread modify private data, direct change in the working space
Thread modifies shared data, copy data to a workspace, modify the workspace, the changes are complete, refresh the memory.
4. Hardware Memory Architecture
CPU cache coherency problem solution:
1. Bus lock: CPU throughput reduction
2. The cache coherence protocol on
When the CPU in the CACHE operation data, if the data is shared variable, in CACHE data read register, a new modified, and update the memory data
CaCHE LINE is set invalid, the other CPU can read data from memory
5. java thread hardware processor
6. The three concurrent programming characteristics
Atomicity: x = 1 indivisible
Visibility: thread can manipulate the data in their own workspace
Ordering: the sequence program is not necessarily the order (re-compiled execution ordering, instruction reordering, Objective: efficiency
JMM three features ensure
- JMM and Atomicity
- X = 10 atomic write if it is a private data having atomic, if the shared data is not atomic (read-write)
- Y = x atomic no
- X workspace data read (atomicity)
- Writes the value of X Y (atomic)
- No atomic I ++
- I read into the workspace
- +1;
- Refresh results to memory
- Z = z + 1 atomic no
- Z read into the workspace
- +1;
- Refresh results to memory
A plurality of atomic operations to merge together without atomicity
Of guarantees:
Synchronized
JUC Lock的lock
JMM与可见性
Volatile
Synchronized:加锁
JUC JUC Lock的lock
- JMM与有序性
Volatile:
Synchronized:
Happens-before原则:
- 程序次序原则
- 锁定原则 :后一次加锁必须等前一次解锁
- Volatile原则:霸道原则
- 传递原则:A---B ---C A--C