Java 多线程(十)ArrayBlockingQueue 阻塞队列

35 篇文章 11 订阅

Java 多线程 系列文章目录:

今天我们来介绍下,Java 中的阻塞队列:ArrayBlockingQueue 

 

阻塞队列和非阻塞的区别:

如果队列里面已经放满了,如果是阻塞队列那么线程会一直阻塞,而非阻塞对垒则会抛出异常。

队列还包括固定长度的队列和不固定长度的队列。

 

ArrayBlockingQueue 类实现了 BlockingQueue 接口,该接口有如下方法:

 
 

 

拿 Insert 情况来说,如果队列里面已经满了,使用 add 方法往里放就会抛出异常。如果 offer 方法返回 false,也就是队列中没有可用空间, put 方法将会阻塞在那里,直到有空间可以放。

下面来看一个例子:

 

public class BlockingQueueTest {
    public static void main(String[] args) {
        //该队列里面只能放3个Integer
        final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
        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();            
    }
}

运行结果如图所示:

 

 

我们可以通过使用两个阻塞队列,容量都是 1 的 ArrayBlockingQueue 来实现同步通知的功能,如:

 

 
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();
            }
        }

        //不能加上synchronized,可能会导致死锁,put方法会阻塞但是不会释放锁
        public void sub(int k){
            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();
            }
        }

        //不能加上synchronized,可能会导致死锁,put方法会阻塞但是不会释放锁
        public void main(int k) {
            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();
            }

        }
    }

这样也能实现主线程和子线程之间的打印切换.

 
 
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chiclaim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值