Home > Back-end >  Talk about all kinds of locks in Java
Talk about all kinds of locks in Java

Time:12-01

The concept of lock in Java a lot, from the perspective of thought, through various locks, divided into the following can let us understand once for them separately,
We usually use are reentrant lock, re-entrant locking device, when a thread for locks, if you want to get a lock, it still can be,
And non-reentrant lock, refers to when a thread to get yourself a lock, if the student to obtain social the lock again, can't get to, non-reentrant lock will lead to a deadlock, so you need to our country is generally not non-reentrant lock in the process of using,
Fair, not fair lock
AQS framework can make fair fair lock and lock, AQS framework consists of three main elements, a state is a state, used to represent the lock is being used, the other one is the current thread, the last one is to save the blocked thread queue,
If used to hold blocked thread queue in order to obtain the lock, and the first thread queue acquire the lock, so it is a fair lock, each thread can be in the order it into the queue for locks,
If a thread is blocked, not to save waiting in line to get a lock, but everyone scrambled to lock, the lock is not fair, this may lead to some threads cannot obtain the lock was starved to death,
Pessimistic locking, optimistic locking, spin locks
Pessimistic locks and optimistic locking is also as a kind of lock, with different cultural dimension
Pessimistic locking refers to Lock before performing operations, to ensure that only gets the Lock the thread to execute, in this case, because the Lock operation, efficiency may be low, but to ensure safety, like synchronized and Reentrant Lock is pessimistic locking,
Optimistic locking is not locked before execution, but when executed judgment, such as the cas is a typical optimistic locking, when executed, will thread working memory with the latest data comparison, if it is update to the latest data before, if not the latest data, the worker thread data in memory has expired, the developer will process the data,
Mentioned CAS, may feel a spin lock, lightweight sync CAS is to use the spin lock lock mode, in this case, we have a good concept of CAS and rotate to distinguish, the CAS is a comparison and exchange, that is, the comparison and exchange, this is an operation, comparison and exchange operations, spin is to point to do the same for many times, they are two different concepts, refers to the spin spin lock way several times with the CAS operation, therefore, the CAS is optimistic locking, and CAS for many times, also is the implementation of the optimistic locking, becomes a spin lock times,
An exclusive lock and a Shared lock
An exclusive lock and a Shared lock is also an idea of cost concept,
There is only one thread can have an exclusive lock the lock device to perform the corresponding operation, such as synchronization and already, and write locks, lock, speaking, reading and writing
Shared lock refers to the enterprise can have multiple threads at the same time, we own the lock, such as: read lock in the read-write lock, allows the use of multiple threads to get together, and Semaphore (signal), because they allow multiple threads need to pass through, so development is a Shared lock,
Conclusion
So many locks the concept of classifying lock from different dimensions, these are conceptual thing, can be in a different way,
These locks is essentially a framework, its function is to allow one or more threads to perform at the right time, and the other one or the other thread block, lock in concurrent Java library, which thread to execute, which thread to block, supported by aqs determined based on the Shared variables and lock,
Yes, the lock is used to control framework and implementation of a thread is blocked,
  • Related