1. Lock
1) appeared after version 1.5, Java.util.concurrent.locks.Lock
2) The lock implementation provides a wider range of locking operations than is possible with synchronized methods and statements. This implementation allows for a more flexible structure that can have very different properties and can support multiple related Condition objects. A lock is a tool that controls the access of multiple threads to a shared resource. Typically, a lock provides exclusive access to a shared resource. Only one thread can get a lock at a time, and all access to the shared resource requires a lock first. However, some locks may allow concurrent access to shared resources, such as read locks for Readwritelock.
3) The code for the general use of lock replacement synchronized is as follows:
Private Lock L = new Reentrantlock (); L.lock (); try {//access the resource protected by this lock} finally {L.unlock (); }
4) Like synchronized, can also communicate between threads, the following code:
class data { private int number = 0;// Shared variables private lock lock = new reentrantlock (); // a mutual exclusion lock private condition c1 = lock.newcondition (); // a bar of the lock private condition c2 = lock.newcondition (); // Another condition of the lock public data () { System.out.println ( C1 == C2); // returns FALSE, a lock can have multiple independent condition } public int increase () { lock.lock ();// must be locked for shared variables, and thread traffic must be based on the locks that are owned try{ if (number ! = 0) { // hereThere is no need to use circular judgment, but object.wait must be judged in the loop try { c1.await (); // If the C1 method is called, then the following conditions should correspond to wake c1 } catch ( Interruptedexception e) { e.printstacktrace (); } } number++; c2.signal (); // wake up c2 in the waiting pool }finally{ lock.unlock (); // All operations after locking must be done using the specified code structure to complete the } return number; } Public int decrease () { lock.lock (); // Operations on shared variables must be locked, and thread traffic must be based on the locks that are owned try{ if (number != 1) {// There is no need to use cyclic judgment, But object.wait must be judged in the loop try { c2.await (); // If the C2 method is called, then the following conditions should correspond to wake c2 } catch (interruptedexception e) { E.printstacktrace (); } } number--; c1.signal (); // wake up c1 }finally{in the waiting pool lock.unlock ();// All operations after locking must be completed using the specified code structure } return number; } }
2. The comparison between Lock and synchronized
1) The JDK 5,synchronized is much slower than Lock, but in JDK 6 they have the same rate of validity.
2) synchronized is a simplified implementation of lock, a lock can correspond to multiple Condition, and synchronized lock and Condition merge, a Synchronized lock corresponds to only one condition, which can be said that synchronized is a simplified version of lock.
3) method comparison, use synchronized when using wait, notify; If you use lock, you need to use condition's await, singal
3. Some sub-classes of lock
1) reentrantlock a reentrant mutex lock, which has the same basic behavior and semantics as the implicit monitor lock accessed using the Synchronized method and the statement, but is more powerful, and generally we can just use this object to get it done!
2) Reentrantreadwritelock.readlock for some read-only operations we can give it a shared lock, which is a read lock that allows multiple threads to read data at the same time, but cannot write data. lock = new Reentrantreadwritelock (). Readlock ();
3) Reentrantreadwritelock.writelock for the write operation, then in order to avoid dirty reading, so we should give him an exclusive lock, so can only write, cannot read, lock = new Reentrantreadwritelock.writelock();
Java review--locks and communication between threads