As we all know, the disadvantage of multithreading is that the code will be the interference of concurrent access, the solution is the synchronization mechanism. The two common forms of synchronization are synchronous code blocks and synchronization functions, and both forms of locks are implicitly locked, and the former holds a lock that is the default.
Starting with the JDK1.5, the lock is encapsulated by the object-oriented idea, providing the lock interface externally and providing an explicit operation on the lock. The lock interface has more operations than synchronized, and is an alternative to synchronization.
The advantage of locking the lock separately is that it allows for more flexible use of locks, and the implementation of the lock interface permits locking to be acquired and freed in a range of scopes, and allows for the acquisition and release of multiple locks in any order, thereby supporting the use of this technique.
Lookup API, the lock interface can be found in the Java.util.concurrent.locks package. The main methods are
Lock ()---> Get lock.
Unlock ()---> Release lock
It should be noted that when the synchronized is synchronized, when the synchronization code is complete, the lock is released automatically, but using the lock interface to achieve synchronization will lose the lock's automatic release function. If the synchronized code block throws an exception, the lock cannot be freed, causing the other threads to fail to execute. Therefore, the unlock operation must be written to the finally clause.
How do I use the lock interface to implement synchronization?
The known implementation class for the lock interface includes Reentrantlock,reentrantreadwritelock,reentrantreadwritelock.writelock.
Lock lock = new Reentrantlock ();
Lock.lock ();
try{
...... ;
}
finally{
Lock.unlock ();
}
If you implement synchronization with the lock interface, the original sync code block or the wait () in the synchronization function, notify (), the Notifyall () method is not available. Based on an object-oriented approach, the original synchronization monitor method is encapsulated in the condition object. And the flexibility of the lock interface is reflected in a lock interface can support multiple condition objects, the corresponding method is await (); signal (); Signalall ().
Use to view an example of an API
Class Boundedbuffer {
final lock lock = new Reentrantlock (); Lock Object
Final Condition notfull = lock.newcondition (); Conditional Object
final Condition notempty = Lock.newcondition ();
Final object[] items = new object[100];
int Putptr, takeptr, Count;
public void put (Object x) throws Interruptedexception {
lock.lock ();//Get lock
try {while
count = = Items.leng TH)
notfull.await ();
ITEMS[PUTPTR] = x;
if (++putptr = = items.length) putptr = 0;
++count;
Notempty.signal ();
} finally {
lock.unlock ();//release Lock
}
} public
Object take () throws Interruptedexception {
Lock.lock ();
try {while
(count = = 0)
notempty.await ();
Object x = items[takeptr];
if (++takeptr = = items.length) takeptr = 0;
--count;
Notfull.signal ();
return x;
} finally {
lock.unlock ();}}}