Reprint Source: http://www.cnblogs.com/lxmyhappy/p/7380073.html
1. What are the locks inJava ?
- Fair Lock / non-fair lock
- Can be re-entered lock
- Exclusive lock / share lock
- Mutex / read/write lock
- optimistic lock / pessimistic lock
- Segmented lock
- bias Lock / Lightweight lock / heavyweight Lock
- Spin lock
Java implements the lock has two kinds of syntax, one is the synchronized statement, the other is the reentrantlock keyword . The above is a lot of the names of locks, these categories are not all refers to the state of the lock, and some of the characteristics of the lock, some of the design of the lock, the following summary of the contents of each lock is a certain explanation of the noun .
Fair Lock / non-fair lock
Fair locking means that multiple threads acquire locks in the order in which they are requested.
A non-fair lock refers to the order in which multiple threads acquire locks not in order of application.
Java Reentranthlock The constructor to specify whether the lock is fair or unfair, the default is a non-fair lock, and for synchronized , it is also an unfair lock.
The advantage of an unfair lock is that the throughput is larger than the fair lock.
Can be re-entered lock
A reentrant lock is also called a recursive lock, which means that when the same thread acquires the lock in the outer layer method, the lock is acquired automatically when it enters the inner layer. Examples are as follows:
synchronized void SetA () throws exception{ Thread.Sleep (+); Setb ();} synchronized void Setb () throws exception{ Thread.Sleep (1000);}
Java Reentrantlock is a reentrant lock. ( The code above is a feature of a reentrant lock, and if it is not a reentrant lock,SETB may not be executed by the current thread, possibly causing a deadlock )
Synchronized is also a re-entry lock.
The advantage of a reentrant lock is the ability to avoid deadlocks to a certain extent.
Exclusive lock / share lock
As the name implies, a unique lock is a lock that can only be held by one thread at a time, and a shared lock may be held by multiple threads.
Java Reentrantlock is a exclusive lock, but for Another implementation of lock Readwritelock, its read lock is a shared lock, and the write lock is a unique lock.
for synchronized is a exclusive lock.
Mutex / read/write lock
On the top, the exclusive lock and the shared lock are generalized, and the mutex and read/write locks are implemented specifically.
Mutex in the specific implementation in Java is reentrantlock.
read/write lock in the concrete implementation in Java is readwritelock.
optimistic lock / pessimistic lock
Optimistic and pessimistic locks do not refer to specific lock types, but rather to the angle of locking problems in concurrent programming.
Pessimistic lock that, for a data concurrency operation, will change the data, even if the actual data has not been changed, but also pessimistic that the possibility of change is relatively large, must be locked, do not lock sooner or later to problems.
Optimistic locking that the concurrency of a data operation, will not change the data, no lock and no problem.
optimistic locking refers to the absence of programming in Java, which is suitable for very many scenarios of read operations.
pessimistic lock refers to the Java , suitable for concurrent write a lot of scenes.
Spin lock
in Java , a spin lock means that the thread that acquires the lock is not immediately blocked, but instead uses a loop to try to acquire the lock, which can enter the critical section when the loop condition is changed by another thread. The advantage is that it reduces the consumption of thread context switches, and the disadvantage is that it consumes CPU.
public class SpinLock { private atomicreference<thread> sign =new atomicreference<> (); public void Lock () { Thread current = Thread.CurrentThread (); while (!sign. Compareandset (null, current)) { } } publicly void Unlock () { Thread current = Thread.CurrentThread (); Sign. Compareandset (current, null); }}
used Cas atomic operation, lock function will owner is set to the current thread, and the original value is predicted to be empty. unlock function will owner set to
when a second thread calls the lock operation because the owner value is not NULL, the loop is always executed until the first thread calls the unlock function to owner set to NULL , the second thread can enter a critical section.
because the spin lock simply keeps the current thread executing the loop body without changing the thread state, the response is faster. However, when the number of threads increases continuously, performance degrades significantly because each thread needs to execute, consuming CPU time. If the thread is not competing fiercely, and the time period of the lock is maintained. Suitable for use with spin locks.
Note: This example is an unfair lock, and the order in which the locks are obtained is not carried out in the order in which they enter lock .
bias Lock / Lightweight lock / heavyweight Lock
these three kinds of locks, that is, refers to the state of the lock, for synchronized.
A biased lock is a piece of code that is always accessed by a thread, so in theory, the thread automatically acquires the lock and has the lock, which reduces the cost of acquiring the lock.
A lightweight lock is when a lock is biased, accessed by another thread, the bias lock is upgraded to a lightweight lock, and other threads try to acquire the lock in a spin, without blocking and increasing efficiency.
A heavy lock is a lightweight lock in the state, while another thread spins, but the choice does not persist, when the spin a certain number of times have not acquired the lock, it will go into the block, the lock will expand into a heavyweight lock, the heavy lock will allow other applications to block the thread, reduce performance.
What classes the Java Lock has (go)