LockSupport

j<wbr style="line-height:25px">ava.util.concurrent.locks.LockSupport</wbr><wbr style="line-height:25px"><br style="line-height:25px"> 是用来创建锁和其他同步类的基本线程阻塞原语。<br style="line-height:25px"><span style="color:#003366; line-height:25px"><wbr style="line-height:25px">此类以及每个使用它的线程与一个许可关联(从Semaphore类的意义上说<wbr style="line-height:25px">)。<br style="line-height:25px"> 可以调用</wbr></wbr></span><span style="color:#ff6600; line-height:25px">park</span><span style="color:#003366; line-height:25px">来使用一个许可,如果有许可可用,将立即返回;否则可能阻塞。当拥有许可时,则可以调用</span><span style="color:#ff6600; line-height:25px">unpark</span><span style="color:#003366; line-height:25px">来释放该许可,使其它线程可用它。</span><br style="line-height:25px"> (但与Semaphore不同的是,许可不能累积,并且最多只能有一个许可。)<br style="line-height:25px"><span style="color:#ff6600; line-height:25px">park</span>和<span style="color:#ff6600; line-height:25px">unpark</span>方法提供了阻塞和解除阻塞线程的有效方法,并且不会遇到导致过时方法Thread.suspend和Thread.resume<br style="line-height:25px"> 因为以下目的变得不可用的问题:由于许可的存在,调用park的线程和另一个试图将其unpark的线程之间的竞争将保持活性。<br style="line-height:25px"><span style="color:#000080; line-height:25px">此外,如果调用者线程被中断,并且支持超时,则park将返回。park方法还可以在其他任何时间“毫无理由”地返回,<br style="line-height:25px"> 因此通常必须在重新检查返回条件的循环里调用此方法</span>。从这个意义上说,park是“忙碌等待”的一种优化,它不会浪费这么多的时间进行自旋,但是必须将它与unpark配对使用才更高效。<br style="line-height:25px"> 三种形式的park还各自支持一个blocker对象参数。此对象在线程受阻塞时被记录,以允许监视工具和诊断工具确定线程受阻塞的原因。<br style="line-height:25px"> (这样的工具可以使用方法<span style="color:#993300; line-height:25px">getBlocker</span>(java.lang.Thread)访问blocker。)建议最好使用这些形式,而不是不带此参数的原始形式。<br style="line-height:25px"> 在锁实现中提供的作为blocker的普通参数是this。<br style="line-height:25px"> 这些方法被设计用来作为创建高级同步实用工具的工具,对于大多数并发控制应用程序而言,它们本身并不是很有用。<br style="line-height:25px"> park方法仅设计用于以下形式的构造:<br style="line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre class="prettyprint" style="line-height:inherit; padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); color:rgb(0,112,0); font-family:monospace; background-color:rgb(250,250,250); margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">while</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(!</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">canProceed</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">())</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">...</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">LockSupport</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">park</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">this</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span></pre> 在这里,在调用park之前,canProceed和其他任何动作都不会锁定或阻塞。<br style="line-height:25px"> 因为每个线程只与一个许可关联,park的任何中间使用都可能干扰其预期效果。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:"此类以及每个使用它的线程与一个许可关联"。"许可"和Semaphore类的"许可"概念是一样的,只是这里只有一个许可。<br style="line-height:25px"> 这里的许可和二进制信号量(Semaphore)的许可一样。具体参见《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920108293346751/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">信号量Semaphore</a></strong>》<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:park方法还可以在其他任何时间"毫无理由"地返回。"毫无理由"太玄了吧。<br style="line-height:25px"> 所以park方法应该放在一个循环中,如果不满足条件,就让它一直循环。<br style="line-height:25px"> 示例用法。以下是一个先进先出(first-in-first-out)非重入锁类的框架。<br style="line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre class="prettyprint" style="line-height:inherit; padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); color:rgb(0,112,0); font-family:monospace; background-color:rgb(250,250,250); margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">class</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">FIFOMutex</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">private</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">final</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">AtomicBoolean</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> locked </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">new</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">AtomicBoolean</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">false</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">private</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">final</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Queue</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> waiters</span><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">new</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">ConcurrentLinkedQueue</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">public</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">void</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">lock</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">boolean</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> wasInterrupted </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">false</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Thread</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> current </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Thread</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">currentThread</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> waiters</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">add</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">current</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Block while not first in queue or cannot acquire lock</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">while</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">waiters</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">peek</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">!=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> current </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">||</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">!</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">locked</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">compareAndSet</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">false</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">true</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">))</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">LockSupport</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">park</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">this</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">if</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Thread</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">interrupted</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">())</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// ignore interrupts while waiting</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> wasInterrupted </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">true</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span></code><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> waiters</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">remove</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">if</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">wasInterrupted</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// reassert interrupt status on exit</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> current</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">interrupt</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">public</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">void</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> unlock</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> locked</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">set</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">false</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">LockSupport</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">unpark</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">waiters</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">peek</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">());</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}}</span></span></pre> <span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:AtomicBooleanlocked和LockSupport的许可的功能,我总觉得几乎是重复的。但是park方法可能毫无理由地返回,<br style="line-height:25px"> 因为这个原因,就不得不用AtomicBooleanlocked了。但是可能这不是唯一原因。<br style="line-height:25px"> 主要函数:<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">unpark(Threadthread)</span><wbr style="line-height:25px"><br style="line-height:25px"> 如果给定线程的许可尚不可用,则使其可用。如果线程在park上受阻塞,则它将解除其阻塞状态。<br style="line-height:25px"> 否则,保证下一次调用park不会受阻塞。如果给定线程尚未启动,则无法保证此操作有任何效果。<br style="line-height:25px"> 参数:<br style="line-height:25px"> thread-要执行unpark操作的线程;该参数为null表示此操作没有任何效果。<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#ff6600; line-height:25px">park(Objectblocker)</span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度,在许可可用之前禁用当前线程。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且该调用立即返回;否则,为线程调度禁用当前线程,<br style="line-height:25px"> 并在发生以下三种情况之一前,使其处于休眠状态:<br style="line-height:25px"> *其他某个线程调用将当前线程作为目标调用unpark;或者<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"> blocker-导致此线程暂停的同步对象<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:"*其他某个线程调用将当前线程作为目标调用unpark",那么其他线程可以取消"当前线程"的park操作。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:"*该调用不合逻辑地(即毫无理由地)返回。",不知道什么时候是这样的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:<span style="color:#ff6600; line-height:25px">Objectblocker</span>。只是为了调试方便。说明是在blocker这里阻塞的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:"*该调用不合逻辑地(即毫无理由地)返回。",因为这个原因FIFOMutex,就不得不用AtomicBooleanlocked了。但是可能这不是唯一原因。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">publicstaticvoidparkNanos(Objectblocker,longnanos)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度,在许可可用前禁用当前线程,并最多等待指定的等待时间。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且该调用立即返回;否则,为线程调度禁用当前线程,并在发生以下四种情况之一前,使其处于休眠状态:<br style="line-height:25px"> *其他某个线程将当前线程作为目标调用unpark;或者<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"> 参数:<br style="line-height:25px"> blocker-导致此线程暂停的同步对象<br style="line-height:25px"> nanos-要等待的最大毫秒数<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">parkUntil(Objectblocker,longdeadline)</span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且该调用立即返回;否则,为线程调度禁用当前线程,并在发生以下四种情况之一前,使其处于休眠状态:<br style="line-height:25px"> *其他某个线程将当前线程作为目标调用unpark;或者<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"> 参数:<br style="line-height:25px"> blocker-导致此线程暂停的同步对象<br style="line-height:25px"> deadline-要等待的绝对时间,用相对于历元(Epoch)的毫秒数值表示<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicstati</span>cObject<span style="color:#ff6600; line-height:25px">getBlocker(Threadt)</span><wbr style="line-height:25px"><br style="line-height:25px"> 返回提供给最近一次尚未解除阻塞的park方法调用的blocker对象,如果该调用不受阻塞,则返回null。<br style="line-height:25px"> 返回的值只是一个瞬间快照,即由于未解除阻塞或者在不同的blocker对象上受阻而具有的线程。<br style="line-height:25px"> 返回:<br style="line-height:25px"> blocker<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">park()</span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度,禁用当前线程,除非许可可用。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且该调用立即返回;否则,为线程调度禁用当前线程,并在发生以下三种情况之一以前,使其处于休眠状态:<br style="line-height:25px"> *其他某个线程将当前线程作为目标调用unpark;或者<br style="line-height:25px"> *其他某个线程中断当前线程;或者<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">publicstaticvoidparkNanos(longnanos)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度禁用当前线程,最多等待指定的等待时间,除非许可可用。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且该调用立即返回;否则,为线程调度禁用当前线程,并在发生以下四种情况之一以前,将其处于休眠状态:<br style="line-height:25px"> *其他某个线程将当前线程作为目标调用unpark;或者<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"> 参数:<br style="line-height:25px"> nanos-要等待的最大毫秒数<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">publicstaticvoidparkUntil(longdeadline)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。<br style="line-height:25px"> 如果许可可用,则使用该许可,并且调用立即返回;否则,为线程调度禁用当前线程,并在发生以下四种情况之一以前,将其处于休眠状态:<br style="line-height:25px"> *其他某个线程将当前线程作为目标调用unpark;或者<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"> 参数:<br style="line-height:25px"> deadline-要等待的绝对时间,用相对于历元的毫秒数值表示</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值