ReentrantLock是Java并发包中一个很是有用的组件,一些并发集合类也是用ReentrantLock实现,包括ConcurrentHashMap。ReentrantLock具备三个特性:等待可中断、可实现公平锁、以及锁能够绑定多个条件。html
ReentrantLock与synchronized关键字同样,属于互斥锁,synchronized中的锁是非公平的(公平锁是指多个线程等待同一个锁时,必须按照申请锁的时间顺序来依次得到锁),ReentrantLock默认状况下也是非公平的,但能够经过带布尔值的构造函数要求使用公平锁。线程经过ReentrantLock的lock()方法得到锁,用unlock()方法释放锁。java
ReentrantLock在等待锁时可使用lockInterruptibly()方法选择中断, 改成处理其余事情,而synchronized关键字,线程须要一直等待下去。一样的,tryLock()方法能够设置超时时间,用于在超时时间内一直获取不到锁时进行中断。并发
ReentrantLock能够实现公平锁,而synchronized的锁是非公平的。ide
ReentrantLock拥有方便的方法用于获取正在等待锁的线程。函数
ReentrantLock能够同时绑定多个Condition对象,而synchronized中,锁对象的wait()和notify()或notifyAll()方法能够实现一个隐含的条件,若是要和多于一个条件关联时,只能再加一个额外的锁,而ReentrantLock只须要屡次调用newCondition方法便可。性能
在JDK1.6以前,ReentrantLock的性能要明显优于synchronized,可是JDK1.6中加入了不少针对锁的优化措施,synchronized和ReentrantLock的性能基本彻底持平了。优化
ReentrantLock的主要缺点是方法须要置于try-finally块中,另外,开发人员须要负责获取和释放锁,而开发人员经常忘记在finally中释放锁。线程
import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; /** * Java program to show, how to use ReentrantLock in Java. * Reentrant lock is an alternative way of locking * apart from implicit locking provided by synchronized keyword in Java. * * @author Javin Paul */ public class ReentrantLockHowto { private final ReentrantLock lock = new ReentrantLock(); private int count = 0; //Locking using Lock and ReentrantLock public int getCount() { lock.lock(); try { System.out.println(Thread.currentThread().getName() + " gets Count: " + count); return count++; } finally { lock.unlock(); } } //Implicit locking using synchronized keyword public synchronized int getCountTwo() { return count++; } public static void main(String args[]) { final ThreadTest counter = new ThreadTest(); Thread t1 = new Thread() { @Override public void run() { while (counter.getCount() < 6) { try { Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }; Thread t2 = new Thread() { @Override public void run() { while (counter.getCount() < 6) { try { Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }; t1.start(); t2.start(); } } Output: Thread-0 gets Count: 0 Thread-1 gets Count: 1 Thread-1 gets Count: 2 Thread-0 gets Count: 3 Thread-1 gets Count: 4 Thread-0 gets Count: 5 Thread-0 gets Count: 6 Thread-1 gets Count: 7
Read more: http://javarevisited.blogspot...code