博客
关于我
狂神JUC——CountDownLatch,CyclicBarrier,Semaphore
阅读量:511 次
发布时间:2019-03-07

本文共 2738 字,大约阅读时间需要 9 分钟。

JUC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

1.CountDownLatch

package com.company.common_assist;public class CountDownLatchTest {       public static void main(String[] args) throws InterruptedException {           //总数是6,必须要执行任务的时候,再使用        java.util.concurrent.CountDownLatch countDownLatch =  new java.util.concurrent.CountDownLatch(5);        for (int i = 1; i <= 6; i++) {               new Thread(()->{                   System.out.println(Thread.currentThread().getName() + "go out");                try {                       countDownLatch.await();                } catch (InterruptedException e) {                       e.printStackTrace();                }            },String.valueOf(i)).start();        }        countDownLatch.await(); //等待计数器归零,然后向下执行        System.out.println("Close Door");    }}

运行结果:

在这里插入图片描述

2.CyclicBarrier

package com.company.common_assist;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;//允许一组线程全部等待彼此到达共用的同步复制public class CyclicBarrierTest {       public static void main(String[] args) {           CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{               System.out.println("召唤神龙");        });        for (int i = 1; i <= 7; i++) {               final int temp = i;            new Thread(()->{                   System.out.println(Thread.currentThread().getName() + "收集第" + temp + "个龙珠");                try {                       cyclicBarrier.await(); //等待                } catch (InterruptedException e) {                       e.printStackTrace();                } catch (BrokenBarrierException e) {                       e.printStackTrace();                }            },String.valueOf(i)).start();        }    }}

运行结果:

在这里插入图片描述

3.Semaphore

package com.company.common_assist;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;//一个计数信号量,在概念上,信号量要维持一组许可证//如果有必要,每个acquire()都会阻塞,直到许可证可用,然后才能使用它//作用:多个共享资源互斥的使用!并发限流,控制最大的线程数public class SemaphoreTest {       public static void main(String[] args) {           //线程数量:停车位!限流!        Semaphore semaphore = new Semaphore(3);        for (int i = 1; i <= 6; i++) {               new Thread(()->{                   try {                       //如果已经满了,会等待资源被释放                    semaphore.acquire();                    System.out.println(Thread.currentThread().getName() + "抢到车位");                    TimeUnit.SECONDS.sleep(2);                    System.out.println(Thread.currentThread().getName() + "离开车位");                } catch (InterruptedException e) {                       e.printStackTrace();                }finally {                       semaphore.release(); //释放,然后唤醒等待的资源                }            },String.valueOf(i)).start();        }    }}

运行结果:

在这里插入图片描述

转载地址:http://jyhjz.baihongyu.com/

你可能感兴趣的文章
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>