1. Basic and package delivery
- Java. Util. Concurrent and contract
- Java. Util. Concurrent. Atomic atomicity
- Java. Util. Concurrent. The locks lock
The difference between concurrency and parallelism: concurrency means that multiple threads share a resource, while parallelism means that different steps are executed simultaneously to do the same thing.
2. Use the Java keyword volatile to import @volatiledemo
Volatile: Lightweight synchronization mechanism provided by the Java virtual machine
- Guaranteed visibility
- Atomicity is not guaranteed
- Disallow command reordering
3. JMM (Java Memory Model)
Key features:
- visibility
- atomic
- order
Order:
Why does volatile prevent instruction reordering? Memory Barrier, a CPU instruction.
Volatile ensures visibility by inserting read and write barriers and, in the case of volatile, by blocking reordering, by memory barriers. Memory barriers can cause instructions to be executed in a particular order. Rules for prohibiting instruction reordering by volatile: 1. When the second operation is a Voaltile write, no reordering can be performed regardless of the first operation. 2. When a local operation is a volatile read, reorder cannot be performed regardless of the second operation 3. Reordering cannot occur when the first operation is volatile write and the second operation is volatile read
This is only specified in the Java Virtual Machine Specification for the interaction between main memory and thread working memory in the memory model to mask the details of data access across different hardware and operating systems. The specific specification is for the bytecode instructions that operate on the JVM, such as the main memory and thread working memory interaction instructions read, load, use, assign, Store, and write. There are different implementations for different versions of JVMS from different platforms and vendors, such as HotSpot, Jrocket, Sun Classic JVMS.
Read rate: Hard disk < Memory < CPU
Due to speed differences, the CPU cannot wait for memory. Therefore, caches exist between the memory and CPU.
Visibility: The working memory of the T1 thread has changed the age copied from the main memory, which is assigned to the main memory. If threads such as T2 and T3 can also be updated to 37, it is visible.
Atomicity: indivisible, either simultaneous success or simultaneous failure
If atomicity is not met, data is lost due to write overwrite by multi-threaded n++ operations
4, the CAS @ CASDemo
What is CAS: Compare and swap. Is a CPU concurrency primitive
When a thread copies data in main memory, it gets an expected value first. After executing data in the thread’s working memory, it checks whether the current value is an expected value. If so, it assigns the executed value to main memory.
Compared with synchronized, CAS without locking not only ensures consistency but also high concurrency.
Underlying CAS principles: Spin locks and UnSafe
AtomicInteger getAndIncrement method:
The UnSafe class:
- The figure above is a spin lock in a thread where var2 (the offset of an attribute in the object) of VAR1 (the address of the object) corresponds to a value var5. Continuous comparison in while:
- Compare var1 and var2, which represent the value at the latest time, and var5, which is the value just obtained. If they are the same, the spin lock is broken and the value is updated. If not, reread the main memory value and assign it to VAR5.
- Var5 is all read in main memory.
- The value var5 is volatile, so it is visible across threads.
Not only integer atomic operations, custom atomic operations are implemented by atomic references: Class AtomicReference<> @ardemo
CAS shortcomings
- Do while spin loop time is long, always occupy CPU overhead
- Atomic operations of only one shared variable are guaranteed
- Develop ABA problems
ABA problem: Thread 2 changed the value of main memory, and then changed it back! Thread 1 doesn’t know that, so thread 1 thinks no one’s touched it.
So how to further solve the ABA problem?
- New mechanism: timestamp
AtomicStampedReference<>
. Equivalent to the version @abademo
5. Class CountDownLatch @countdownlatchdemo
Action: Controls the last thread to perform the last operation after all other threads have completed. Decrement to 0 wakes up the last thread
CyclicBarrier @cyclicbarrierDemo
Action: Controls the last thread to perform the last operation after all other threads have completed. Add to the barrier to wake up the last thread
Class Semaphore @semaphoredemo
2. Used primarily for two purposes:
- Mutually exclusive use of multiple shared resources
- Control of the number of concurrent threads
The third way to create a thread is callable@callableDemo
Thread pool @mythreadpooldemo
Seven parameters of ThreadPoolExecutor: