Lock is a Java multi-threading synchronization mechanism that controls the thread's access to shared resources. A thread must obtain a lock before executing a synchronous method or block of code.
Lock's Lock () and Unlock () method;
Lock (): Gets a lock that if the lock is not available, the current thread is disabled for thread scheduling purposes and is dormant until the lock is acquired.
Unlock (): Release the acquired lock.
Lock's scope of action:
If Lock is static, the scope is the entire class.
Public classTest { Public Static voidMain (string[] args) {Thread Thread_one=NewThread (NewAccount (), "Thread_one"); Thread Thread_two=NewThread (NewAccount (), "Thread_tow"); Thread_one.start (); Thread_two.start (); }}classAccountImplementsrunnable{StaticLock lock =NewReentrantlock ();//Static Lock Static intCount = 0; @Override Public voidrun () {runtest (); } Public voidruntest () {lock.lock (); Try{ for(inti = 0; I < 5; ++i) {Count++; System.out.println (Thread.CurrentThread (). GetName ()+ " " +Count); } } finally{lock.unlock (); } }}//Output://Thread_one 1//Thread_one 2//Thread_one 3//Thread_one 4//Thread_one 5//Thread_tow 6//Thread_tow 7//Thread_tow 8//Thread_tow 9//Thread_tow Ten
Static Lock
If Lock is non-static, the range is the entire object.
ImportJava.util.concurrent.locks.Lock;ImportJava.util.concurrent.locks.ReentrantLock; Public classTest1 { Public Static voidMain (string[] args) {Thread Thread_one=NewThread (NewAccount (), "Thread_one"); Thread Thread_two=NewThread (NewAccount (), "Thread_tow"); Thread_one.start (); Thread_two.start (); }}classAccountImplementsrunnable{Lock Lock=NewReentrantlock ();//non-static lock Static intCount = 0; @Override Public voidrun () {runtest (); } Public voidruntest () {lock.lock (); Try{ for(inti = 0; I < 5; ++i) {Count++; System.out.println (Thread.CurrentThread (). GetName ()+ " " +Count); } } finally{lock.unlock (); } }}//Output://Thread_tow 2//Thread_one 2//Thread_tow 3//Thread_one 4//Thread_tow 5//Thread_one 6//Thread_tow 7//Thread_one 8//Thread_tow 9//Thread_one Ten
non-static lock
A simple implementation of lock
Public classlock{Private BooleanisLocked =false; Public synchronized voidLock ()throwsinterruptedexception{ while(isLocked) {//use Whlie instead of ifWait ();//terminates the thread and releases the lock on the object, and when the thread is notified and restarted, the lock is automatically retrieved} isLocked=true; } Public synchronized voidunlock () {isLocked=false; Notify ();//notifies a waiting thread to regain the lock and resume execution }}
the method here is lock (), and if the current lock is locked by another thread, this calls the Wait () method, which lets the thread wait until another thread calls the Notify () method in Unlock () and restarts. This uses the while instead of the if, to prevent wait () exit after the lock is still locked by other threads (such as the lock is snatched by other threads),(Lock () and notify (), Notifyall () is the method in the object objects)
Reentrantlock is an implementation of the lock interface.
Public class Implements Lock, java.io.serializable{}
The Reentrantlock is reentrant in nature.
What is the re-entry of cocoa? Let me give you an example.
Public class reentrant2{ new// lock with the public class lock {} outer () { lock.lock (); Inner (); Lock.unlock (); } Public synchronized inner () { lock.lock (); // Do something Lock.unlock (); }}
Assuming that a thread invokes the outer () method, the thread invokes lock (), acquires the lock, and then calls inner (), but the lock () acquires the lock again within inner (), but it is locked by the first call, so it enters the while call to wait ( ) thereby causing a deadlock.
Reentrant allows a thread to invoke a lock held by itself (if the lock is reentrant).
Rewrite the lock code
Public classlock{BooleanisLocked =false; Thread Lockedby=NULL; intLockedcount = 0; Public synchronized voidLock ()throwsinterruptedexception{Thread Callingthread=Thread.CurrentThread (); while(isLocked && Lockedby! = callingthread) {//If the lock is locked by a lock that is not held by oneselfwait (); } isLocked=true; Lockedcount++; Lockedby=Callingthread; } Public synchronized voidunlock () {if(Thread.curentthread () = = This. Lockedby) {Lockedcount--; if(Lockedcount = = 0) {isLocked=false; Notify (); } } } ...}
The while loop adds a judgment lockedby! = Callingthread, so that it is not locked by its own lock, and is counted with Lockedcount, only if Lockedcount is 0 o'clock, the Notify () method is called.
Finally, it is best to use a try when using a lock, in case some unexpected problem causes the lock to not release.
Lock.lock (); Try { //docritical sections code, which may throw exceptionfinally { Lock.unlock ();}
Reference: http://tutorials.jenkov.com/java-concurrency/locks.html
Java Multithreaded Learning Chapter (III) Lock