【张孝祥并发课程笔记】14:java5阻塞队列的应用

1、Semaphore
Semaphore可以维护当前访问自身的线程个数,并且提供了同步机制。
比如当前有5个在线客服,有12个会员需要接待。同时就只能有5个人被接待。当任何一个服务完成后,等待中的其他人就可以占用服务了。

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        final Semaphore sp = new Semaphore(3);
        for (int i = 0; i < 10; i++) {
            Runnable runnable = new Runnable() {
                public void run() {
                    sp.acquire();//必须用try..catch包起来
                    //TODO //线程需要处理的事情
                    sp.release();
                }
            };
            service.execute(runnable);
        }
    }

2、CyclicBarrier
一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarrier 很有用。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        final  CyclicBarrier cb = new CyclicBarrier(3);
        for(int i=0;i<3;i++){
            Runnable runnable = new Runnable(){
                    public void run(){
                    try {
                        Thread.sleep((long)(Math.random()*10000));  
                        System.out.println(Thread.currentThread().getName() +"已经到了第一站");

                        cb.await();

                        Thread.sleep((long)(Math.random()*10000));
                        System.out.println(Thread.currentThread().getName() +"已经到了第二站");
                        cb.await();

                        Thread.sleep((long)(Math.random()*10000));
                        System.out.println(Thread.currentThread().getName() +"已经到了第三站");
                        cb.await();                     
                    } catch (Exception e) {
                        e.printStackTrace();
                    }               
                }
            };
            service.execute(runnable);
        }
        service.shutdown();
    }

3、CountDownLatch同步工具
好像倒计时计数器,调用CountDownLatch对象的countDown方法就将计数器减1,当到达0时,所有等待者就开始执行。

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        final CountDownLatch cdOrder = new CountDownLatch(1);
        final CountDownLatch cdAnswer = new CountDownLatch(3);
        for (int i = 0; i < 3; i++) {
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() + "运动员准备中");
                        cdOrder.await();//运动员各就位倒计时中
                        System.out.println(Thread.currentThread().getName() + "运动员开跑");
                        Thread.sleep((long) (Math.random() * 10000));    //开始处理
                        System.out.println(Thread.currentThread().getName() + "运动员跑完了");
                        cdAnswer.countDown();//裁判准备
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            service.execute(runnable);
        }
        try {
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println(Thread.currentThread().getName() + "准备发布命令");
            cdOrder.countDown();
            System.out.println(Thread.currentThread().getName() + "已发布命令");
            cdAnswer.await();//裁判准备收到比赛结果
            System.out.println(Thread.currentThread().getName() + "已收到所有响应");
        } catch (Exception e) {
            e.printStackTrace();
        }
        service.shutdown();

    }

4、Exchanger同步工具
用于实现两个人之间的数据交换,每个人在完成一定的事务后想与对方交换数据,第一个先拿出数据的人会一直等待第二个人,直到第二个人拿着数据到来时,才能彼此交换数据。

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        final Exchanger exchanger = new Exchanger();
        service.execute(new Runnable() {
            public void run() {
                try {

                    String data1 = "宝贝111";
                    System.out.println(Thread.currentThread().getName() + "正在把数据" + data1 + "换出去");
                    Thread.sleep((long) (Math.random() * 10000));
                    String data2 = (String) exchanger.exchange(data1);
                    System.out.println(Thread.currentThread().getName() + "换回的数据为" + data2);
                } catch (Exception e) {

                }
            }
        });
        service.execute(new Runnable() {
            public void run() {
                try {
                    String data1 = "宝贝222";
                    System.out.println(Thread.currentThread().getName() + "正在把数据" + data1 + "换出去");
                    Thread.sleep((long) (Math.random() * 10000));
                    String data2 = (String) exchanger.exchange(data1);
                    System.out.println(Thread.currentThread().getName() + "换回的数据为" + data2);
                } catch (Exception e) {

                }
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值