java ReentrantLock详解
介绍
ReentrantLock称为重入锁,比内部锁synchonized拥有更强大的功能,它可中断、可定时、设置公平锁
【注】使用ReentrantLock时,一定要释放锁,一般释放放到finnal里写。
提供以下重要的方法
- lock():获得锁,如果锁已被占用,则等待
- lockInterruptibly():获得锁,但有限响应中断
- unlock():释放锁
- tryLock():尝试获取锁。如果获得,返回true;否则返回false
- tryLock(longtime,TimeUnitunit):在给定时间内获得锁。如果获得返回true;否则返回false
示例
例子1
importjava.util.concurrent.locks.ReentrantLock; publicclassReentrantLockTest{ ReentrantLocklock; ReentrantLockTest(ReentrantLocklock){ this.lock=lock; } privateRunnablegetRunnable(){ returnnewRunnable(){ @Override publicvoidrun(){ while(true){ try{ if(lock.tryLock()){ try{ System.out.println("Locked:"+Thread.currentThread().getName()); Thread.sleep(800); }finally{ lock.unlock(); System.out.println("UnLocked:"+Thread.currentThread().getName()); } System.out.println("breakbefore"); break; }else{ //System.out.println("Unabletolock"+Thread.currentThread().getName()); } }catch(InterruptedExceptione){ System.out.println(Thread.currentThread()+"isInterupted"); e.printStackTrace(); } } } }; } publicstaticvoidmain(String[]args){ ReentrantLocklock=newReentrantLock(); ReentrantLockTesttest=newReentrantLockTest(lock); ReentrantLockTesttest2=newReentrantLockTest(lock); Threadthread1=newThread(test.getRunnable(),"firstThread"); Threadthread2=newThread(test2.getRunnable(),"secondThread"); thread1.start(); thread2.start(); try{ Thread.sleep(300); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("interuptbegin"); thread2.interrupt(); System.out.println("interuptend"); } }
一次执行结果:
Locked:firstThread
interuptbegin
interuptend
UnLocked:firstThread
breakbefore
Locked:secondThread
UnLocked:secondThread
Thread[secondThread,5,main]isInterupted
java.lang.InterruptedException:sleepinterrupted
atjava.lang.Thread.sleep(NativeMethod)
atcom.jihite.templet.JavaBase.ReentrantLockTest$1.run(ReentrantLockTest.java:23)
atjava.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:secondThread
breakbefore
分析:firstThread执行,secondThread不停的判断是否可以获得锁,当firstThread执行完,secondThread执行后被打断
例子2
importjava.util.concurrent.TimeUnit; importjava.util.concurrent.locks.ReentrantLock; publicclassReentrantLockTest{ ReentrantLocklock; ReentrantLockTest(ReentrantLocklock){ this.lock=lock; } privateRunnablegetRunnable(){ returnnewRunnable(){ @Override publicvoidrun(){ while(true){ try{ if(lock.tryLock(700,TimeUnit.MILLISECONDS)){ try{ System.out.println("Locked:"+Thread.currentThread().getName()); Thread.sleep(800); }finally{ lock.unlock(); System.out.println("UnLocked:"+Thread.currentThread().getName()); } System.out.println("breakbefore"); break; }else{ //System.out.println("Unabletolock"+Thread.currentThread().getName()); } }catch(InterruptedExceptione){ System.out.println(Thread.currentThread()+"isInterupted"); e.printStackTrace(); } } } }; } publicstaticvoidmain(String[]args){ ReentrantLocklock=newReentrantLock(); ReentrantLockTesttest=newReentrantLockTest(lock); ReentrantLockTesttest2=newReentrantLockTest(lock); Threadthread1=newThread(test.getRunnable(),"firstThread"); Threadthread2=newThread(test2.getRunnable(),"secondThread"); thread1.start(); thread2.start(); try{ Thread.sleep(300); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("interuptbegin"); thread2.interrupt(); System.out.println("interuptend"); } }
一次执行结果
Locked:firstThread
interuptbegin
interuptend
Thread[secondThread,5,main]isInterupted
java.lang.InterruptedException
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireNanos(AbstractQueuedSynchronizer.java:936)
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireNanos(AbstractQueuedSynchronizer.java:1247)
atjava.util.concurrent.locks.ReentrantLock.tryLock(ReentrantLock.java:442)
atcom.jihite.templet.JavaBase.ReentrantLockTest$1.run(ReentrantLockTest.java:19)
atjava.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:firstThread
breakbefore
UnLocked:secondThread
breakbefore
分析:firstThread执行,secondThread等待,等待过程被打断。打断后firstThread执行结束了,secondThread得到锁,继续执行
例子3
importjava.util.concurrent.locks.ReentrantLock; publicclassReentrantLockTest2{ ReentrantLocklock; ReentrantLockTest2(ReentrantLocklock){ this.lock=lock; } privateRunnablegetRunnable(){ returnnewRunnable(){ @Override publicvoidrun(){ while(true){ try{ try{ lock.lock(); //lock.lockInterruptibly(); System.out.println("Locked:"+Thread.currentThread().getName()); Thread.sleep(800); break; }finally{ lock.unlock(); System.out.println("UnLocked:"+Thread.currentThread().getName()); } }catch(InterruptedExceptione){ e.printStackTrace(); } } } }; } publicstaticvoidmain(String[]args){ ReentrantLocklock=newReentrantLock(); ReentrantLockTest2test=newReentrantLockTest2(lock); ReentrantLockTest2test2=newReentrantLockTest2(lock); Threadthread1=newThread(test.getRunnable(),"firstThread"); Threadthread2=newThread(test2.getRunnable(),"secondThread"); thread1.start(); thread2.start(); try{ Thread.sleep(600); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("interuptbegin"); thread2.interrupt(); System.out.println("interuptend"); } }
一次执行结果
Locked:firstThread
interuptbegin
interuptend
UnLocked:firstThread
Locked:secondThread
UnLocked:secondThread
java.lang.InterruptedException:sleepinterrupted
atjava.lang.Thread.sleep(NativeMethod)
atcom.jihite.templet.JavaBase.ReentrantLockTest2$1.run(ReentrantLockTest2.java:22)
atjava.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:secondThread
分析:firstThread先获得锁执行,secondThread在等待,此时中断并未打断等待。firstThread执行完,secondThread获取后被打断
例子4
publicclassReentrantLockTest2{ ReentrantLocklock; ReentrantLockTest2(ReentrantLocklock){ this.lock=lock; } privateRunnablegetRunnable(){ returnnewRunnable(){ @Override publicvoidrun(){ while(true){ try{ try{ //lock.lock(); lock.lockInterruptibly(); System.out.println("Locked:"+Thread.currentThread().getName()); Thread.sleep(800); break; }finally{ lock.unlock(); System.out.println("UnLocked:"+Thread.currentThread().getName()); } }catch(InterruptedExceptione){ e.printStackTrace(); } } } }; } publicstaticvoidmain(String[]args){ ReentrantLocklock=newReentrantLock(); ReentrantLockTest2test=newReentrantLockTest2(lock); ReentrantLockTest2test2=newReentrantLockTest2(lock); Threadthread1=newThread(test.getRunnable(),"firstThread"); Threadthread2=newThread(test2.getRunnable(),"secondThread"); thread1.start(); thread2.start(); try{ Thread.sleep(600); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("interuptbegin"); thread2.interrupt(); System.out.println("interuptend"); } }
一次执行结果
Locked:firstThread
interuptbegin
interuptend
Exceptioninthread"secondThread"java.lang.IllegalMonitorStateException
atjava.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
atjava.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
atcom.jihite.templet.JavaBase.ReentrantLockTest2$1.run(ReentrantLockTest2.java:25)
atjava.lang.Thread.run(Thread.java:748)
分析:lock.lockInterruptibly();在执行过程中可以响应中断时间
以上所述是小编给大家介绍的javaReentrantLock详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!