Use Lock locks for thread synchronization

Lock lock: locked locked on local needs
1) after the new function JDK1.5
2) compared with Synchronized, Lock offers a variety of lock solutions, more flexible
3) Java.util.concurrent.lock in Lock is an interface, its implementation class is a Java class, rather than as features of the language (keywords) to achieve
NOTE: If there is abnormal synchronization code, to finally put in unLock0

 

Use Step
1) Creating Lock Objects
2) method call lock0 locked
3) call unlock0 unlock method

 

Lock synchronized with the difference
. 1) Lock is a lock (lock manually opened and closed, do not forget to lock off), synchronized implicit locks
2) Lock block only locks, synchronized with a lock and method lock block
3) Lock Lock , the JVM will spend less time to schedule threads, better performance and better scalability (more subclasses)
when 4) Lock positioned to ensure that when a thread critical section of code, another thread does not enter critical section. If another thread tries to enter the lock code, it will have to wait (ie stop) until the object is released. lock0 method will be locked Lock instance of an object, all calls to the object lock) method thread will be blocked until unlock0 method object is called Lock

 

Code Example:

 

 

 

Guess you like

Origin www.cnblogs.com/LuJunlong/p/12158929.html