现在的位置: 首页 > 综合 > 正文

Java5 多线程(八)– ArrayBlockingQueue阻塞队列

2013年10月07日 ⁄ 综合 ⁄ 共 3334字 ⁄ 字号 评论关闭
阻塞队列和非阻塞的区别:如果队列里面已经放满了,如果是阻塞队列那么线程会一直阻塞,而非阻塞对垒则会抛出异常.
队列还包括固定长度的队列和不固定长度的队列.
这个类实现了BlockingQueue
这个接口有如下方法:


拿Insert情况来说,如果队列里面已经满了,使用add方法往里放就会抛出异常,用offer放回false,用put()方法将会阻塞在那里,知道有空间可以放.
下面来看一个例子:
public class BlockingQueueTest {

    public static void main(String[] args) {
        final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);//该队列里面只能放3个Integer
        for(int i=0;i<2;i++){
            new Thread(){
                public void run(){
                    while(true){
                        try {
                            Thread.sleep((long)(Math.random()*1000));
                            System.out.println(Thread.currentThread().getName() + "准备放数据!");                            
                            queue.put(1);//如果放满就会阻塞
                            System.out.println(Thread.currentThread().getName() + "已经放了数据," +                             
                                        "队列目前有" + queue.size() + "个数据");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
        new Thread(){
            public void run(){
                while(true){
                    try {
                        //将此处的睡眠时间分别改为100和1000,观察运行结果
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName() + "准备取数据!");
                        queue.take();//如果没有了数据,就会阻塞
                        System.out.println(Thread.currentThread().getName() + "已经取走数据," +                             
                                "队列目前有" + queue.size() + "个数据");                    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
 
        }.start();            
    }

}

运行结果如图:


我们可以通过使用两个都是一个控件的缓冲队列来实现同步通知的功能.
 
static class Business {

        BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
        BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
 
        {//设置匿名构造方法,他会在实例化对象前执行.
            try {
                //因为要让主线程先执行.
                queue2.put(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public void sub(int k){//不能加上synchronized,可能会导致死锁,put方法会阻塞但是不会释放锁
            try {
                queue2.put(1);//第一次执行的时候因为已经满了,就阻塞
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println("sub thread sequence " + i + " loop of " + k);
            }
            try {
                queue1.take();//这样queue1就可以put了
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public void main(int k) {//不能加上synchronized,可能会导致死锁,put方法会阻塞但是不会释放锁
            try {
                queue1.put(1);//可以放
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 1; i <= 100; i++) {
                System.out.println("main thread sequence " + i + " loop of " + k);
            }
            try {
                queue2.take();//这样queue2就可以put了
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
        }

    }

这样也能实现主线程和子线程之间的打印切换.
转载请注明出处 : http://blog.csdn.net/johnny901114/article/details/8696026

抱歉!评论已关闭.