java并发容器之 SynchronousQueue [转]

SynchronousQueue 这个队列实现了 BlockingQueue接口

该队列的特点 

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

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

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) {http://www.huiyi8.com/chajian/

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


        queue.put(1);
        queue.put(2);
        queue.put(3);
    }
posted @ 2016-03-30 15:58  songjinduo  阅读(148)  评论(0编辑  收藏  举报