`
kanwoerzi
  • 浏览: 1647734 次
文章分类
社区版块
存档分类
最新评论

互斥锁ReentrantLock

 
阅读更多
publicclass<wbr style="line-height:25px">ReentrantLock</wbr><wbr style="line-height:25px"><br style="line-height:25px"> extendsObject<br style="line-height:25px"> implementsLock,Serializable<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">一个可重入的互斥锁Lock,它具有与使用synchronized方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ReentrantLock</span>将由最近成功获得锁,并且还没有释放该锁的线程所拥有。当锁没有被另一个线程所拥有时,<br style="line-height:25px"> 调用lock的线程将成功获取该锁并返回。如果当前线程已经拥有该锁,此方法将立即返回。<br style="line-height:25px"> 可以使用isHeldByCurrentThread()和getHoldCount()方法来检查此情况是否发生。<br style="line-height:25px"> 此类的构造方法接受一个可选的公平参数。当设置为<span style="color:#0000ff; line-height:25px">true</span>时,在多个线程的争用下,<br style="line-height:25px"> 这些锁倾向于将访问权授予等待时间最长的线程。否则此锁将无法保证任何特定访问顺序。<br style="line-height:25px"> 与采用默认设置(使用不公平锁)相比,使用<span style="color:#000080; line-height:25px">公平锁</span>的程序在许多线程访问时表现为很低的总体吞吐量(即速度很慢,常常极其慢),<br style="line-height:25px"> 但是在获得锁和保证锁分配的均衡性时差异较小。不过要注意的是,公平锁不能保证线程调度的公平性。<br style="line-height:25px"> 因此,使用公平锁的众多线程中的一员可能获得多倍的成功机会,这种情况发生在其他活动线程没有被处理并且目前并未持有锁时。<br style="line-height:25px"> 还要注意的是,未定时的tryLock方法并没有使用公平设置。因为即使其他线程正在等待,只要该锁是可用的,此方法就可以获得成功。<br style="line-height:25px"> 建议总是立即实践,使用lock块来调用try,在之前/之后的构造中,<span style="line-height:25px"><wbr style="line-height:25px">最典型的代码</wbr></span><wbr style="line-height:25px">如下:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">X</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatefinal</span><span style="color:#3366ff; line-height:25px">ReentrantLocklock=newReentrantLock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//...</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidm(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">lock.lock();</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//blockuntilconditionholds</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//...methodbody</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">finally</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">lock.unlock()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"> 除了实现Lock接口,此类还定义了isLocked和getLockQueueLength方法,以及一些相关的protected访问方法,<br style="line-height:25px"> 这些方法对检测和监视可能很有用。<br style="line-height:25px"> 该类的序列化与内置锁的行为方式相同:一个反序列化的锁处于解除锁定状态,不管它被序列化时的状态是怎样的。<br style="line-height:25px"> 此锁最多支持同一个线程发起的2147483648个递归锁。试图超过此限制会导致由锁方法抛出的Error。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:此类的构造方法接受<span style="color:#000080; line-height:25px"><wbr style="line-height:25px">一个可选的公平参数</wbr></span><wbr style="line-height:25px">。当设置为<span style="color:#0000ff; line-height:25px">true</span>时,在多个线程的争用下,<br style="line-height:25px"> 这些锁倾向于将访问权授予等待时间最长的线程。否则此锁将无法保证任何特定访问顺序。但是公平锁不能保证线程调度的公平性。<br style="line-height:25px"> 因此,使用公平锁的众多线程中的一员可能获得多倍的成功机会。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">2:此锁是个<span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">递归锁</wbr></span><wbr style="line-height:25px">。此锁最多支持同一个线程发起的2147483648个递归锁。关于递归锁的问题可参照《可递归锁与非递归锁》<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">主要函数</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">lock()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 获取锁。<br style="line-height:25px"> 如果该锁没有被另一个线程保持,则获取该锁并立即返回,将锁的保持计数设置为1。<br style="line-height:25px"> 如果当前线程已经保持该锁,则将保持计数加1,并且该方法立即返回。<br style="line-height:25px"> 如果该锁被另一个线程保持,则出于线程调度的目的,禁用当前线程,并且在获得锁之前,该线程将一直处于休眠状态,此时锁保持计数被设置为1。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的lock<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:"如果该锁没有被另一个线程保持,则获取该锁并立即返回,将锁的保持计数设置为1。<br style="line-height:25px"> 如果当前线程已经保持该锁,则将保持计数加1,并且该方法立即返回。"<br style="line-height:25px"> 这样就实现了递归锁.<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">lockInterruptibly<wbr style="line-height:25px">()</wbr></span><br style="line-height:25px"> throwsInterruptedException<br style="line-height:25px"><br style="line-height:25px"> 如果当前线程未被中断,则获取锁。<br style="line-height:25px"> 如果该锁没有被另一个线程保持,则获取该锁并立即返回,将锁的保持计数设置为1。<br style="line-height:25px"> 如果当前线程已经保持此锁,则将保持计数加1,并且该方法立即返回。<br style="line-height:25px"> 如果锁被另一个线程保持,则出于线程调度目的,禁用当前线程,并且在发生以下两种情况之一以前,该线程将一直处于休眠状态:<br style="line-height:25px"> *锁由当前线程获得;或者<br style="line-height:25px"> *其他某个线程中断当前线程。<br style="line-height:25px"> 如果当前线程获得该锁,则将锁保持计数设置为1。<br style="line-height:25px"> 如果当前线程:<br style="line-height:25px"> *在进入此方法时已经设置了该线程的中断状态;或者<br style="line-height:25px"> *在等待获取锁的同时被中断。<br style="line-height:25px"> 则抛出InterruptedException,并且清除当前线程的已中断状态。<br style="line-height:25px"> 在此实现中,因为此方法是一个显式中断点,所以要优先考虑响应中断,而不是响应锁的普通获取或重入获取。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的lockInterruptibly<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果当前线程已中断。<br style="line-height:25px"> 注意:它会抛出InterruptedException。<br style="line-height:25px"> 关于InterruptedException更多知识请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920108154244111/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">JAVA线程的interrupt</a></strong>》。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#ff6600; line-height:25px">tryLock()</span><br style="line-height:25px"> 仅在调用时锁未被另一个线程保持的情况下,才获取该锁。<br style="line-height:25px"> 如果该锁没有被另一个线程保持,并且立即返回true值,则将锁的保持计数设置为1。<br style="line-height:25px"> 即使已将此锁设置为使用公平排序策略,但是调用tryLock()仍将立即获取锁(如果有可用的),<br style="line-height:25px"> 而不管其他线程当前是否正在等待该锁。在某些情况下,此“闯入”行为可能很有用,即使它会打破公平性也如此。<br style="line-height:25px"> 如果希望遵守此锁的公平设置,则使用tryLock(0,TimeUnit.SECONDS),它几乎是等效的(也检测中断)。<br style="line-height:25px"> 如果当前线程已经保持此锁,则将保持计数加1,该方法将返回true。<br style="line-height:25px"> 如果锁被另一个线程保持,则此方法将立即返回false值。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的tryLock<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果锁是自由的并且被当前线程获取,或者当前线程已经保持该锁,则返回true;否则返回false<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:该方法不会阻塞,它也不一定能得到锁。如果得到返回ture,否则false.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">2:即使已将此锁设置为使用公平排序策略,但是调用tryLock()仍将立即获取锁(如果有可用的),<br style="line-height:25px"> 而不管其他线程当前是否正在等待该锁。这样看来他获得锁的优先级比正被阻塞的lock()方法高。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#ff6600; line-height:25px">tryLock</span>(longtimeout,<br style="line-height:25px"> TimeUnitunit)<br style="line-height:25px"> throwsInterruptedException<br style="line-height:25px"> 如果锁在给定等待时间内没有被另一个线程保持,且当前线程未被中断,则获取该锁。<br style="line-height:25px"> 如果该锁没有被另一个线程保持,并且立即返回true值,则将锁的保持计数设置为1。<br style="line-height:25px"> 如果为了使用公平的排序策略,已经设置此锁,并且其他线程都在等待该锁,则不会获取一个可用的锁。<br style="line-height:25px"> 这与tryLock()方法相反。如果想使用一个允许闯入公平锁的定时tryLock,那么可以将定时形式和不定时形式组合在一起:<br style="line-height:25px"> if(lock.tryLock()||lock.tryLock(timeout,unit)){...}<br style="line-height:25px"> 如果当前线程已经保持此锁,则将保持计数加1,该方法将返回true。<br style="line-height:25px"> 如果锁被另一个线程保持,则出于线程调度目的,禁用当前线程,并且在发生以下三种情况之一以前,该线程将一直处于休眠状态:<br style="line-height:25px"> *锁由当前线程获得;或者<br style="line-height:25px"> *其他某个线程中断当前线程;或者<br style="line-height:25px"> *已超过指定的等待时间<br style="line-height:25px"> 如果获得该锁,则返回true值,并将锁保持计数设置为1。<br style="line-height:25px"> 如果当前线程:<br style="line-height:25px"> *在进入此方法时已经设置了该线程的中断状态;或者<br style="line-height:25px"> *在等待获取锁的同时被中断。<br style="line-height:25px"> 则抛出InterruptedException,并且清除当前线程的已中断状态。<br style="line-height:25px"> 如果超出了指定的等待时间,则返回值为false。如果该时间小于等于0,则此方法根本不会等待。<br style="line-height:25px"> 在此实现中,因为此方法是一个显式中断点,所以要优先考虑响应中断,而不是响应锁的普通获取或重入获取,或者报告所用的等待时间。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的tryLock<br style="line-height:25px"> 参数:<br style="line-height:25px"> timeout-等待锁的时间<br style="line-height:25px"> unit-timeout参数的时间单位<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果锁是自由的并且由当前线程获取,或者当前线程已经保持该锁,则返回true;如果在获取该锁之前已经到达等待时间,则返回false<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果当前线程被中断<br style="line-height:25px"> NullPointerException-如果时间单位为null<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:如果为了使用公平的排序策略,已经设置此锁,并且其他线程都在等待该锁,<br style="line-height:25px"> 则不会获取一个可用的锁。这与tryLock()方法相反<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">2:它会抛出InterruptedException。<br style="line-height:25px"> 关于InterruptedException更多知识请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920108154244111/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">JAVA线程的interrupt</a></strong>》。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">unlock()</span><wbr style="line-height:25px"><br style="line-height:25px"> 试图释放此锁。<br style="line-height:25px"> 如果当前线程是此锁所有者,则将保持计数减1。如果保持计数现在为0,则释放该锁。<br style="line-height:25px"> 如果当前线程不是此锁的持有者,则抛出IllegalMonitorStateException。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的unlock<br style="line-height:25px"> 抛出:<br style="line-height:25px"> IllegalMonitorStateException-如果当前线程没有保持此锁<br style="line-height:25px"> 注意1:"如果当前线程是此锁所有者,则将保持计数减1。如果保持计数现在为0,则释放该锁。"这样就实现了递归锁.<br style="line-height:25px"> 下面两个是对于<span style="line-height:25px"><wbr style="line-height:25px">调试和测试很有用的方法</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">getHoldCount<wbr style="line-height:25px">()</wbr></span><br style="line-height:25px"> 查询当前线程保持此锁的次数。<br style="line-height:25px"> 对于与解除锁操作不匹配的每个锁操作,线程都会保持一个锁。<br style="line-height:25px"> 保持计数信息通常只用于测试和调试。例如,如果不应该使用已经保持的锁进入代码的某一部分,则可以声明如下:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">X</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> ReentrantLocklock=newReentrantLock();<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//...</span><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">m(){<br style="line-height:25px"> assertlock.getHoldCount()==0;<br style="line-height:25px"> lock.lock();<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//...methodbody</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">finally</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.unlock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 返回:<br style="line-height:25px"> 当前线程保持此锁的次数,如果此锁未被当前线程保持过,则返回0<br style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">isHeldByCurrentThread<wbr style="line-height:25px">()</wbr></wbr></span><br style="line-height:25px"> 查询当前线程是否保持此锁。<br style="line-height:25px"> 与内置监视器锁的Thread.holdsLock(java.lang.Object)方法类似,此方法通常用于调试和测试。<br style="line-height:25px"> 例如,只在保持某个锁时才应调用的方法可以声明如下:<br style="line-height:25px"> classX{<br style="line-height:25px"> ReentrantLocklock=newReentrantLock();<br style="line-height:25px"><span style="color:#808080; line-height:25px">//...</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span>m(){<br style="line-height:25px"> assertlock.isHeldByCurrentThread();<br style="line-height:25px"><span style="color:#808080; line-height:25px">//...methodbody</span><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> 还可以用此方法来确保某个重入锁是否以非重入方式使用的,例如:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">X{<br style="line-height:25px"> ReentrantLocklock=newReentrantLock();<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//...</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">m(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">assert!lock.isHeldByCurrentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.lock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//...methodbody</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">finally</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.unlock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果当前线程保持此锁,则返回true;否则返回false<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicCondition</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">newCondition()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 返回用来与此Lock实例一起使用的Condition实例。<br style="line-height:25px"> 在使用内置监视器锁时,返回的Condition实例支持与Object的监视器方法(wait、notify和notifyAll)相同的用法。<br style="line-height:25px"> *在调用Condition、waiting或signalling这些方法中的任意一个方法时,如果没有保持此锁,则将抛出IllegalMonitorStateException。<br style="line-height:25px"> *在调用waiting条件方法时,将释放锁,并在这些方法返回之前,重新获取该锁,将锁保持计数恢复为调用方法时所持有的值。<br style="line-height:25px"> *如果线程在等待时被中断,则等待将终止,并将抛出InterruptedException,清除线程的中断状态。<br style="line-height:25px"> *等待线程按FIFO顺序收到信号。<br style="line-height:25px"> *等待方法返回的线程重新获取锁的顺序与线程最初获取锁的顺序相同,在默认情况下,未指定此顺序,但对于公平锁,它们更倾向于那些等待时间最长的线程。<br style="line-height:25px"> 指定者:<br style="line-height:25px"> 接口Lock中的newCondition<br style="line-height:25px"> 返回:<br style="line-height:25px"> Condition对象<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:关于Condition的更多知识和使用请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201082812035668/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Condition</a></strong>》。<br style="line-height:25px"> Lock的使用很简单,见实例1。<wbr style="line-height:25px">关于Lock的Condition使用请查阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201082812035668/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Condition</a></strong>》<wbr style="line-height:25px"><br style="line-height:25px"> 实例1:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">LockDemo()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Countercounter=newCounter();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("initvalue:"+counter.getValue());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">counter.increment();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("thevalue:"+counter.getValue());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">counter.decrement();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">counter.decrement();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">counter.decrement();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("thevalue:"+counter.getValue());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Counter</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateintvalue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatefinalReentrantLocklock=newReentrantLock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">getValue()</span><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.lock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnvalue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.unlock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("thehodecountoflockis:"+lock.getHoldCount());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px">increment()</span><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.lock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return++value;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.unlock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px">decrement()</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.lock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return--value;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">lock.unlock();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

    详解Java多线程编程中互斥锁ReentrantLock类的用法

    Java多线程并发的程序中使用互斥锁有synchronized和ReentrantLock两种方式,这里我们来详解Java多线程编程中互斥锁ReentrantLock类的用法:

    Java多线程 ReentrantLock互斥锁详解

    主要介绍了Java多线程 ReentrantLock互斥锁详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    locks框架_ReentrantLock.pdf

    解释为什么它被称为“可重入锁”,以及如何解决传统锁可能的问题。 ReentrantLock 的基本用法: 深入探讨如何使用 ReentrantLock 来保护共享资源。演示如何通过 lock 和 unlock 方法来实现线程的同步和互斥。 ...

    Java多线程并发编程(互斥锁Reentrant Lock)

    主要介绍了ReentrantLock 互斥锁,在同一时间只能被一个线程所占有,在被持有后并未释放之前,其他线程若想获得该锁只能等待或放弃,需要的朋友可以参考下

    读写锁.txt

    ReentrantLock//互斥锁 class CachedData { Object data; volatile boolean cacheValid; ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();

    高级开发并发面试题和答案.pdf

    互斥锁(重量级锁)也称为阻塞同步、悲观锁; 为什么说重量级锁开销大呢; synchronize实现基础syn为什么一定有可重入特性; synchronized 实现可重入性; reenlock和synchronize区别; ReentrantLock如何实现可重入性...

    去故新 Java线程新同步机制

    1、可重入锁ReentrantLock,相当于synchronized块,为临界区提供互斥访问机制。 (1) 相关的接口 创建一个可重入锁 Lock lock = new ReentrantLock(); 请求锁,如果锁被当前另一个线程持有,则阻塞。 void ...

    java并发编程面试题

    java并发编程 基础知识,守护线程与线程, 并行和并发有什么区别? 什么是上下文切换?...ReentrantLock(重入锁)实现原理与公平锁非公平锁区别什么是可重入锁(ReentrantLock)? ThreadLocal内存泄漏分析与

    基于redis实现分布式锁的原理与方法

    为了保证一个在高并发存场景下只能被同一个线程操作,java并发处理提供ReentrantLock或Synchronized进行互斥控制。但是这仅仅对单机环境有效。我们实现分布式锁大概通过三种方式。 redis实现分布式锁 数据库实现...

    locks框架:接口.pdf

    可重入性和重入锁: 解释 Lock 接口的可重入性,讲解同一个线程多次获取锁的机制,避免死锁。介绍 ReentrantLock 的实现原理。 Condition 条件变量: 介绍 Lock 接口中的 Condition,它可以实现更复杂的线程等待和...

    java多线程安全性基础介绍.pptx

    关键共享资源上互斥,变并发为串行即同步 锁 分类 显示锁 Lock Lock是个接口 实现类 ReentrantLock 可重入锁 ReentrantReadWriteLock.ReadLock ReentrantReadWriteLock.WriteLock 隐式锁(内置锁) ...

    个人总结的深入java多线程开发

    7)ReentrantLock可重入的互斥锁定 Lock 32 8)阻塞队列BlockingQueue 34 9)已完成任务队列CompletionService 36 10)计时器CountDownLatch 37 11)周期性同步工具CyclicBarrier 38 12)异步计算的结果Future 40 13)安排...

    java核心知识点整理.pdf

    1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM .........................

    JAVA核心知识点整理(有效)

    1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM ........................

Global site tag (gtag.js) - Google Analytics