SynchronousQueue
来源:互联网 发布:竞价排名优化方法 编辑:程序博客网 时间:2024/04/30 02:19
package concurrentTest;import java.util.Arrays;import java.util.List;import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;public class TestSynchronousQueue { class Producer implements Runnable { private BlockingQueue<String> queue; List<String> objects = Arrays.asList("one", "two", "three"); public Producer(BlockingQueue<String> q) { this.queue = q; } public void run() { try { for (String s : objects) { queue.put(s);// 产生数据放入队列中 System.out.printf("put:%s%n",s); } queue.put("Done");// 已完成的标志 } catch (InterruptedException e) { e.printStackTrace(); } } } class Consumer implements Runnable { private BlockingQueue<String> queue; public Consumer(BlockingQueue<String> q) { this.queue = q; } public void run() { String obj = null; try { while (!((obj = queue.take()).equals("Done"))) { System.out.println(obj);//从队列中读取对象 Thread.sleep(3000); //故意sleep,证明Producer是put不进去的 } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { BlockingQueue<String> q=new SynchronousQueue<String>(); TestSynchronousQueue t=new TestSynchronousQueue(); new Thread(t.new Producer(q)).start(); new Thread(t.new Consumer(q)).start(); }}
0 0
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- SynchronousQueue
- java SynchronousQueue
- Java ArrayBlockingQueue, SynchronousQueue
- 集合框架 Queue---SynchronousQueue
- SynchronousQueue简单demo
- 简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
- 简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
- 简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
- SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue区别
- SynchronousQueue 与 BlockingQueue
- 阻塞队列之SynchronousQueue
- 13 种激励程序员的方法
- Capsule:开源的 JVM 应用部署工具
- rust Ownership,Reference and Lifetime
- struts 拦截器
- eclipse 插件
- SynchronousQueue
- swift中的函数的使用,嵌套
- ActiveMQ讯息策略
- 重写equals方法和hashcode方法
- 【Mockplus教程】分组
- zookeeper的基本分布式工作原理
- 键盘弹出高度和遮挡问题
- 名词解释---Bitrate/比特率
- IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)