CyclicBarrier

本文深入探讨了Java集合包中的CyclicBarrier类,详细解释了它的用途、工作原理及如何在并行处理中实现高效同步。重点阐述了CyclicBarrier如何在一组线程间建立循环等待屏障,确保所有线程在特定点同步执行任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.util.concurrent.CyclicBarrie<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">r</span><span style="color:#003366; line-height:25px">是一个同步辅助类,它允许一组线程互相等待,直到</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">所有到达</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">,然后他们才可以跨越屏障点(commonbarrierpoint)。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时CyclicBarrier很有用。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">因为该barrier在释放等待线程后可以重用,所以称它为循环的barrier。</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">CyclicBarrier</span><span style="color:#000080; line-height:25px">支持一个可选的Runnable 对象参数</span>,在一组线程中的最后一个线程到达之后执行它(但在释放所有线程之前),<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">1:CyclicBarrier支持一个可选的Runnable,在一组线程中的最后一个线程到达之后(但在释放所有线程之前)执行Runnable<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例用法</wbr></span><wbr style="line-height:25px">:下面是一个在并行分解设计中使用barrier的例子:<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,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)">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)">Solver</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)">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(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> N</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)">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(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">float</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)"> data</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)">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)">CyclicBarrier</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> barrier</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)">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)">Worker</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)">implements</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)">Runnable</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)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> myRow</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)">Worker</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)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> row</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)"> myRow </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)"> row</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)">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)"> run</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)">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,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">done</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)"> processRow</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)">myRow</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)">try</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)"> barrier</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)">await</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)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">catch</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)">InterruptedException</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> ex</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)">return</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)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">catch</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)">BrokenBarrierException</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> ex</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)">return</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><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><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(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Solver</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)">float</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)"> matrix</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)"> data </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)"> matrix</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)"> N </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)"> matrix</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)">length</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)"> barrier </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)">CyclicBarrier</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)">N</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)">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)">Runnable</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)">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)"> run</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)"> mergeRows</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><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)">for</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,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> i </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,102,102)"><span class="lit" style="line-height:23px; color:rgb(0,102,102)">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)"> i </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">&lt;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> N</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)">i</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)">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)">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,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)">Worker</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)">i</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)">start</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)"> waitUntilDone</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> 在这个例子中,每个worker线程处理矩阵的一行,在处理完所有的行之前,该线程将一直在屏障处等待。<br style="line-height:25px"> 处理完所有的行之后,将执行所提供的Runnable屏障操作,并合并这些行。<br style="line-height:25px"> 如果合并者确定已经找到了一个解决方案,那么done()将返回true,所有的worker线程都将终止。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:这个例子感觉不怎么好。done()和waitUntilDone()对阅读代码干扰很大。<br style="line-height:25px"> 如果屏障操作在执行时不依赖于正挂起的线程,则线程组中的任何线程在获得释放时都能执行该操作。<br style="line-height:25px"> 为方便此操作,每次调用await()都将返回能到达屏障处的线程的序列号。然后,您可以选择哪个线程应该执行屏障操作,例如:<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)">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)">barrier</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)">await</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(0,102,102)"><span class="lit" style="line-height:23px; color:rgb(0,102,102)">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)"><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)">// log the completion of this iteration</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">:<wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">barrier.await()==barrier.getParties()-1</span><span style="color:#000080; line-height:25px">表示是第一个到达的线程,</span><span style="color:#0000ff; line-height:25px">barrier.await()==0</span><span style="color:#000080; line-height:25px">表示这是最后一个到达的线程,当然此时它已经跨过了屏障<wbr style="line-height:25px">。</wbr></span><span style="color:#000080; line-height:25px">至于线程是第几个跨越障碍的线程,这个不清楚,应该和线程的调度有关。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">可以参照实例1</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 对于失败的同步尝试,CyclicBarrier使用了一种要么全部要么全不(all-or-none)的破坏模式:<br style="line-height:25px"> 如果因为中断、失败或者超时等原因,导致线程过早地离开了屏障点,<br style="line-height:25px"> 那么在该屏障点等待的其他所有线程也将通过BrokenBarrierException(如果它们几乎同时被中断,则用InterruptedException)以反常的方式离开。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:如果因为中断、失败或者超时等原因,导致线程跨过了屏障点,那么其他线程将过BrokenBarrierException的方式跨过。(如果它们几乎同时被中断,则用InterruptedException) <div style="line-height:25px"> <span style="line-height:16px; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px">Memory consistency effects: Actions in a thread prior to calling<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">await()</code><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/package-summary.html#MemoryVisibility" style="color:rgb(0,102,153); line-height:23px; text-decoration:none"><span style="line-height:23px">happen-before</span></a>actions that are part of the barrier action, which in turn<span style="line-height:23px">happen-before</span>actions following a successful return from the corresponding<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">await()</code>in other threads.</span><br style="line-height:25px"> await()一定比在它之前的代码被执行,await()一定比它之后的代码先被执行;await()之前的代码一定比Runnable barrierAction先被执行,Runnable barrierAction一定比await()之后的代码先被执行</div> <div style="line-height:25px"> <span style="line-height:25px">主要方法</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">CyclicBarrier</wbr></span><wbr style="line-height:25px">(intparties,<br style="line-height:25px"> RunnablebarrierAction)<br style="line-height:25px"> 创建一个新的CyclicBarrier,它将在给定数量的参与者(线程)处于等待状态时启动,并在启动barrier时执行给定的屏障操作,该操作由最后一个进入barrier的线程执行。<br style="line-height:25px"> 参数:<br style="line-height:25px"> parties-在启动barrier前必须调用await()的线程数<br style="line-height:25px"> barrierAction-在启动barrier时执行的命令;如果不执行任何操作,则该参数为null<br style="line-height:25px"> 抛出:<br style="line-height:25px"> IllegalArgumentException-如果parties小于1<br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">CyclicBarrier</span><wbr style="line-height:25px">(intparties,<br style="line-height:25px"> RunnablebarrierAction)<br style="line-height:25px"> 创建一个新的CyclicBarrier,它将在给定数量的参与者(线程)处于等待状态时启动,并在启动barrier时执行给定的屏障操作,该操作由最后一个进入barrier的线程执行。<br style="line-height:25px"> 参数:<br style="line-height:25px"> parties-在启动barrier前必须调用await()的线程数<br style="line-height:25px"> barrierAction-在启动barrier时执行的命令;如果不执行任何操作,则该参数为null<br style="line-height:25px"> 抛出:<br style="line-height:25px"> IllegalArgumentException-如果parties小于1<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">getParties<wbr style="line-height:25px">()</wbr></wbr></span><br style="line-height:25px"> 返回要求启动此barrier的参与者数目。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 要求启动此barrier的参与者数目<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">await()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> throwsInterruptedException,<br style="line-height:25px"> BrokenBarrierException<br style="line-height:25px"> 在所有参与者都已经在此barrier上调用await方法之前,将一直等待。<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"> *其他某个线程在等待barrier时超时;或者<br style="line-height:25px"> *其他某个线程在此barrier上调用reset()。<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"> 如果在线程处于等待状态时barrier被reset(),或者在调用await时barrier被损坏,抑或任意一个线程正处于等待状态,<br style="line-height:25px"> 则抛出BrokenBarrierException异常。<br style="line-height:25px"> 如果任何线程在等待时被中断,则其他所有等待线程都将抛出BrokenBarrierException异常,并将barrier置于损坏状态。<br style="line-height:25px"> 如果当前线程是最后一个将要到达的线程,并且构造方法中提供了一个非空的屏障操作,则在允许其他线程继续运行之前,<br style="line-height:25px"> 当前线程将运行该操作。如果在执行屏障操作过程中发生异常,则该异常将传播到当前线程中,并将barrier置于损坏状态。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 到达的当前线程的序列号,其中,getParties()-1指示将到达的第一个线程,零指示最后一个到达的线程<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果当前线程在等待时被中断<br style="line-height:25px"> BrokenBarrierException-如果另一个线程在当前线程等待时被中断或超时,或者重置了barrier,<br style="line-height:25px"> 或者在调用await时barrier被损坏,抑或由于异常而导致屏障操作(如果存在)失败。<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">返回是"到达的当前线程的序列号",但是本质表示在当前线程到达时,还需要await几个线程。</span><br style="line-height:25px"><span style="color:#0000ff; line-height:22px">barrier.await()==barrier.getParties()-1</span><span style="color:#000080; line-height:22px">表示是第一个到达的线程,</span><span style="color:#0000ff; line-height:22px">barrier.await()==0</span><span style="color:#000080; line-height:22px">表示这是最后一个到达的线程,当然此时它已经跨过了屏障<wbr style="line-height:22px">。</wbr></span><span style="color:#000080; line-height:22px">至于线程是第几个跨越障碍的线程,这个不清楚,应该和线程的调度有关。</span></wbr></wbr></wbr></wbr></wbr> </div> <div style="line-height:25px"> <span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">2:"如果当前线程不是将到达的最后一个线程,出于调度目的,将禁用它。"这里的禁用应该是指不分配时间片段给它吧。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px">await</span>(longtimeout,<br style="line-height:25px"> TimeUnitunit)<br style="line-height:25px"> throwsInterruptedException,<br style="line-height:25px"> BrokenBarrierException,<br style="line-height:25px"> TimeoutException<br style="line-height:25px"> 在所有参与者都已经在此屏障上调用await方法之前将一直等待,或者超出了指定的等待时间。<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"> *其他某个线程在等待barrier时超时;或者<br style="line-height:25px"> *其他某个线程在此barrier上调用reset()。<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"> 如果超出指定的等待时间,则抛出TimeoutException异常。如果该时间小于等于零,则此方法根本不会等待。<br style="line-height:25px"> 如果在线程处于等待状态时barrier被reset(),或者在调用await时barrier被损坏,<br style="line-height:25px"> 抑或任意一个线程正处于等待状态,则抛出BrokenBarrierException异常。<br style="line-height:25px"> 如果任何线程在等待时被中断,则其他所有等待线程都将抛出BrokenBarrierException,并将屏障置于损坏状态。<br style="line-height:25px"> 如果当前线程是最后一个将要到达的线程,并且构造方法中提供了一个非空的屏障操作,<br style="line-height:25px"> 则在允许其他线程继续运行之前,当前线程将运行该操作。如果在执行屏障操作过程中发生异常,<br style="line-height:25px"> 则该异常将传播到当前线程中,并将barrier置于损坏状态。<br style="line-height:25px"> 参数:<br style="line-height:25px"> timeout-等待barrier的时间<br style="line-height:25px"> unit-超时参数的时间单位<br style="line-height:25px"> 返回:<br style="line-height:25px"> 到达的当前线程的索引,其中,索引getParties()-1指示第一个将要到达的线程,零指示最后一个到达的线程<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果当前线程在等待时被中断<br style="line-height:25px"> TimeoutException-如果超出了指定的超时时间<br style="line-height:25px"> BrokenBarrierException-如果另一个线程在当前线程等待时被中断或超时,或者重置了barrier,<br style="line-height:25px"> 或者调用await时barrier被损坏,抑或由于异常而导致屏障操作(如果存在)失败。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#ff6600; line-height:25px">isBroken()</span><br style="line-height:25px"> 查询此屏障是否处于损坏状态。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果因为构造或最后一次重置而导致中断或超时,从而使一个或多个参与者摆脱此barrier,<br style="line-height:25px"> 或者因为异常而导致某个屏障操作失败,则返回true;否则返回false。<br style="line-height:25px"> publicvoidreset()<br style="line-height:25px"> 将屏障重置为其初始状态。如果所有参与者目前都在屏障处等待,则它们将返回,<br style="line-height:25px"> 同时抛出一个BrokenBarrierException。<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"> 与为后续使用创建一个新barrier相比,这种方法可能更好一些。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px">getNumberWaiting()</span><br style="line-height:25px"> 返回当前在屏障处等待的参与者数目。此方法主要用于调试和断言。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 当前阻塞在await()中的参与者数目。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.BrokenBarrierException;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.CyclicBarrier;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">CyclicBarrierDemo</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">CyclicBarrierDemo</span><span style="color:#3366ff; line-height:25px">()<br style="line-height:25px"> {<br style="line-height:25px"> intnumber=5;<br style="line-height:25px"> CyclicBarrierbarrier=newCyclicBarrier(number);<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(inti=0;i&lt;number;i++)</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">newCyclicBarrierDemoThread("thread"+i,barrier).start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(100);</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">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</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">e.printStackTrace();</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:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">CyclicBarrierDemoThread</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Thread</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">privateCyclicBarrierbarrier;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">CyclicBarrierDemoThread</span><span style="color:#3366ff; line-height:25px">(Stringname,CyclicBarrierbarrier)</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">super(name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.barrier=barrier;</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">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</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">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(getName()+"hasarrived");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intval=barrier.await();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(getName()+"hascrossdthebarrier"+val);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</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">e.printStackTrace();</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">catch</span><span style="color:#3366ff; line-height:25px">(BrokenBarrierExceptione)</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">e.printStackTrace();</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"><wbr style="line-height:25px">运行结果<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thread0hasarrived<br style="line-height:25px"> thread1hasarrived<br style="line-height:25px"> thread2hasarrived<br style="line-height:25px"> thread3hasarrived<br style="line-height:25px"> thread4hasarrived<br style="line-height:25px"> thread0hascrossdthebarrier4<br style="line-height:25px"> thread1hascrossdthebarrier3<br style="line-height:25px"> thread4hascrossdthebarrier0<br style="line-height:25px"> thread3hascrossdthebarrier1</span><br style="line-height:25px"><br style="line-height:25px"></wbr></wbr></wbr> </div> </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、付费专栏及课程。

余额充值