Mutex locked in AQS Mutex and shared locks of It has been introduced in detail in , A lock can only be held by one thread at a time , Other threads cannot , Unless the thread holding the lock releases the lock . Why is mutex mentioned here ？ In fact, both mutex and spinlock are synchronization schemes , The final effect is the same , But they handle threads that don't get locks differently . For mutexes , When a thread holds a lock , Another thread will be blocked . Similar to a mutex , Spinlocks ensure that public data can only be obtained by one thread at any time , The difference is that the spin lock will adopt the spin processing mode after the lock acquisition failure .
Spin lock is a non blocking lock , Its core mechanism is the word spin , That is, using spin operation instead of blocking operation . When a thread tries to acquire a lock , If the lock is already occupied by another thread , The thread will repeatedly check whether the lock is released , Instead of letting this thread hang or sleep . Once another thread releases the lock , This thread can obtain the lock . Spin is a busy waiting state , It will be consumed all the time CPU Time slice of .
Mutex has a big drawback , That is, after the lock acquisition failure, the thread will enter sleep or block state , This process involves scheduling from user mode to kernel mode , Context switching costs a lot . If a lock is locked for a short time , At this point, if the lock acquisition fails, it will be a little bit more than the gain to make it sleep or block , Because it's probably more expensive than spinning . In summary, mutex is more suitable for holding locks for a long time , The spin lock is more suitable for holding the lock for a short time .
Spin lock features
The core mechanism of spin lock is death and so on , All the threads that want to get the lock are trying to get the lock , Of course, it also leads to competition problems .
Like mutexes , Spinlocks also allow only one thread to acquire the lock .
Spin lock can provide interrupt mechanism , Because it doesn't get stuck , So it supports interrupts very well .
Spin lock is suitable for short lock holding time scenarios , The critical area of lock protection is very small , This is easy to understand , If you hold the lock for too long , So it's possible that a lot of threads are spinning , Waste a lot CPU resources .
Spinlocks don't guarantee fairness , There is no guarantee of a first come, first served lock , This can cause thread starvation .
Spinlocks need to ensure the consistency of local cache data , On multiprocessor machines , The processor corresponding to each thread reads and writes the same variable . Each write operation needs to synchronize each processor cache , This may affect performance .
Spin lock example
Let's see a simple implementation of spin lock , It mainly depends on lock and unlock Two methods ,Unsafe Just hardware level atoms for operations CAS operation . about lock Method , Suppose there are several threads competing , Be able to pass successfully CAS Operation modification value The value is newV Is the thread that successfully acquires the lock . It will go through , And other threads are constantly looping around value Whether the value is changed back to 0, take value Change it to 0 The operation is to release the lock after the thread that obtains the lock is executed . about unlock Method , Used to release locks , After release, several threads continue to compete for the lock . In this way , Threads that do not acquire locks are not suspended or blocked , It's a cycle of checking the state .
AQS The spin mechanism of
AQS In the framework, whether mutual exclusive lock or shared lock implementation is based on the spin mechanism , But it optimizes spin locking , This will be explained later . For example, the following two pictures are AQS The logic of framework acquiring exclusive lock and shared lock , We don't have to worry about the specific logic , It's mainly about what's in the box for(;;) This line of code . This is spin manipulation , Spin through infinite cycles
Java Concurrent programming
- Java Concurrent programming ：Java How serialization works
- Java Concurrent programming ： The formation condition and processing of deadlock in concurrency
- Java Concurrent programming ： process 、 Threads 、 Parallelism and concurrency
- Java Concurrent programming ： Task executor Executor Interface
- Java Concurrent programming ：AQS Mutex and shared locks of
- Java Concurrent programming ： What is? JDK Built in concurrency framework AQS
- Java Concurrent programming ：AQS How to guarantee the atomicity of
- Java Concurrent programming ： How to prevent deadlock during thread blocking and wake-up
- Java Concurrent programming ： How to realize blocking and wake-up in multithreading
- Java Concurrent programming ：AQS The spin lock of
本文为[Code farm architecture]所创，转载请带上原文链接，感谢