SynchronousQueue

SynchronousQueue是这样<wbr style="line-height:25px">一种阻塞队列,其中每个put必须等待一个take,反之亦然。同步队列没有任何内部容量,甚至连一个队列的容量都没有。</wbr>
不能在同步队列上进行peek,因为仅在试图要取得元素时,该元素才存在;
除非另一个线程试图移除某个元素,否则也不能(使用任何方法)添加元素;
也不能迭代队列,因为其中没有元素可用于迭代。<wbr style="line-height:25px">队列的头是尝试添加到队列中的首个已排队线程元素;如果没有已排队线程,则不添加元素并且头为null</wbr><wbr style="line-height:25px">。</wbr>

对于其他Collection方法(例如contains),SynchronousQueue作为一个空集合。此队列不允许null元素。
同步队列类似于CSP和Ada中使用的rendezvous信道。
它非常适合于传递性设计,在这种设计中,在一个线程中运行的对象要将某些信息、
事件或任务传递给在另一个线程中运行的对象,它就必须与该对象同步。
对于正在等待的生产者和使用者线程而言,此类支持可选的公平排序策略。默认情况下不保证这种排序。
但是,使用公平设置为true所构造的队列可保证线程以FIFO的顺序进行访问。公平通常会降低吞吐量,但是可以减小可变性并避免得不到服务。
构造函数
Public Constructors
<nobr style="line-height:21px"></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/SynchronousQueue.html#SynchronousQueue()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SynchronousQueue</a></span>()</nobr>
Creates a SynchronousQueuewith nonfair access policy.
<nobr style="line-height:21px"></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/SynchronousQueue.html#SynchronousQueue(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SynchronousQueue</a></span>(boolean fair)</nobr>
Creates a SynchronousQueuewith the specified fairness policy.
<wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:它一种阻塞队列,其中每个put必须等待一个take,反之亦然。<br style="line-height:25px"> 同步队列没有任何内部容量,甚至连一个队列的容量都没有。<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">:不允许使用null元素。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:公平排序策略是指调用put的线程之间,或take的线程之间。<br style="line-height:25px"> 公平排序策略可以查考ArrayBlockingQueue中的公平策略。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:SynchronousQueue的以下方法很有趣:<br style="line-height:25px"> *iterator()永远返回空,因为里面没东西。<br style="line-height:25px"> *peek()永远返回null。<br style="line-height:25px"> *put()往queue放进去一个element以后就一直wait直到有其他thread进来把这个element取走。<br style="line-height:25px"> *offer()往queue里放一个element后立即返回,如果碰巧这个element被另一个thread取走了,offer方法返回true,认为offer成功;否则返回false。<br style="line-height:25px"> *offer(2000,TimeUnit.SECONDS)往queue里放一个element但是等待指定的时间后才返回,返回的逻辑和offer()方法一样。<br style="line-height:25px"> *take()取出并且remove掉queue里的element(认为是在queue里的。。。),取不到东西他会一直等。<br style="line-height:25px"> *poll()取出并且remove掉queue里的element(认为是在queue里的。。。),只有到碰巧另外一个线程正在往queue里offer数据或者put数据的时候,该方法才会取到东西。否则立即返回null。<br style="line-height:25px"> *poll(2000,TimeUnit.SECONDS)等待指定的时间然后取出并且remove掉queue里的element,其实就是再等其他的thread来往里塞。<br style="line-height:25px"> *isEmpty()永远是true。<br style="line-height:25px"> *remainingCapacity()永远是0。<br style="line-height:25px"> *remove()和removeAll()永远是false。<br style="line-height:25px"><span style="line-height:25px">实例1</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Random;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.SynchronousQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.TimeUnit;</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">Test</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">SynchronousQueue&lt;String&gt;queue=newSynchronousQueue();</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//TODOAuto-generatedmethodstub</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;10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadProducer(queue)).start();</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;10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadConsumer(queue)).start();</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">ThreadProducer</span><span style="color:#3366ff; line-height:25px">implementsRunnable</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">ThreadProducer(SynchronousQueue&lt;String&gt;queue)</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">this.queue=queue;</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">SynchronousQueue&lt;String&gt;queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticintcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidrun()</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">Stringname="";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intval=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Randomrandom=newRandom(System.currentTimeMillis());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</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">cnt=(cnt+1)&amp;0xFFFFFFFF;</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">val=random.nextInt()%15;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(val&lt;5)</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">name="offername:"+cnt;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">queue.offer(name);</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">elseif</span><span style="color:#3366ff; line-height:25px">(val&lt;10)</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">name="putname:"+cnt;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">queue.put(name);</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">else</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">name="offerwaittimeandname:"+cnt;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">queue.offer(name,1000,TimeUnit.MILLISECONDS);</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">Thread.sleep(1);</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:#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">ThreadConsumer</span><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Runnable<br style="line-height:25px"> {<br style="line-height:25px"> ThreadConsumer(SynchronousQueue&lt;String&gt;queue)<br style="line-height:25px"> {<br style="line-height:25px"> this.queue=queue;<br style="line-height:25px"> }<br style="line-height:25px"> SynchronousQueue&lt;String&gt;queue;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()<br style="line-height:25px"> {<br style="line-height:25px"> Stringname;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)<br style="line-height:25px"> {<br style="line-height:25px"> try{<br style="line-height:25px"> name=queue.take();<br style="line-height:25px"> System.out.println("take"+name);<br style="line-height:25px"> Thread.sleep(1);<br style="line-height:25px"> }catch(InterruptedExceptione)<br style="line-height:25px"> {<br style="line-height:25px"> e.printStackTrace();<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"></span><span style="color:#3366ff; line-height:25px"><wbr style="line-height:25px"></wbr></span></wbr></wbr></wbr></wbr></wbr>
SynchronousQueue是BlockingQueue的一种实现。关于BlockingQueue的更多内容请参考《 BlockingQueue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值