Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23090
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 134
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 15:58
文章分类

全部博文(17)

文章存档

2014年(17)

我的朋友

分类: JavaScript

2014-05-24 17:56:09

SynchronousQueue 这个队列实现了 BlockingQueue接口

该队列的特点 

1.容量为0,无论何时 size方法总是返回0

2. put操作阻塞,  直到另外一个线程取走队列的元素。

3.take操作阻塞,直到另外的线程put某个元素到队列中。

4. 任何线程只能取得其他线程put进去的元素,而不会取到自己put进去的元素

public static void main(String[] args) throws InterruptedException {
        final SynchronousQueue queue = new SynchronousQueue();


        new Thread() {
            @Override
            public void run() {
                try {
                    while (true) {
                        System.out.println("size1:" + queue.size());
                        System.out.println("element:" + queue.take());
                        System.out.println("size2:" + queue.size());


                    }
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }.start();


        queue.put(1);
        queue.put(2);
        queue.put(3);
    }
阅读(388) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~