java并发工具

14 篇文章 0 订阅

CountDownLatch

作用:当线程执行一定的数量之后,才继续执行后序代码

CountDownLatch countDownLatch = new CountDownLatch(3);//需要执行的线程数量
new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println(111);
        countDownLatch.countDown();//计数
    }
}).start();
new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println(222);
        countDownLatch.countDown();//计数
    }
}).start();
new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println(333);
        countDownLatch.countDown();//计数
    }
}).start();
countDownLatch.await();//数量未达到,不往下执行
System.out.println("三个线程执行完毕");

CyclicBarrier

作用:循环栅栏,将线程分组

public static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);//三个为一组
public static void main(String[] args) throws Exception {
    for (int i = 0; i < 5; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(111);
                try {
                    cyclicBarrier.await();//计数
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(222);
                try {
                    cyclicBarrier.await();//计数
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(333);
                try {
                    cyclicBarrier.await();//计数
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}

结果:

222
111
333
333
111
222
222
111
222
333
333
111
333
222
111

Process finished with exit code 0

Semaphore

作用:控制并发数

public static Semaphore semaphore = new Semaphore(3);//最大资源申请数

public static void main(String[] args) throws Exception {
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    semaphore.acquire();//申请资源
                    System.out.println(111);
                    semaphore.release();//释放资源
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    semaphore.acquire();//申请资源
                    System.out.println(444);
                    semaphore.release();//释放资源
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    semaphore.acquire();//申请资源
                    System.out.println(333);
                    semaphore.release();//释放资源
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
}

Phaser

作用:更灵活的工具
new Phaser(3).register()
动态添加一个或多个参与者,同时返回phase值作抵达分类用。

new Phaser(3).bulkRegister(int parties)
将指定数目的参与者注册到phaser中,所有这些新的参与者都将被当成没有执行完本阶段的线程。

new Phaser(3).arriveAndAwaitAdvance()
类似await()方法,记录到达线程数,阻塞等待其他线程到达同步点后再继续执行。

new Phaser(3).arriveAndDeregister()
动态撤销线程在phaser的注册,通知phaser对象,该线程已经结束该阶段且不参与后面阶段。由此减少了未来phase上需要前进的线程数量。

new Phaser(3).arrive()
通知phaser该线程已经完成该阶段,但不等待其他线程。必须小心使用这个方法,因为它不会与其他线程同步。

new Phaser(3).isTerminated()
phaser是否被终止

Exchanger

作用:两个线程进行数据交换

public static Exchanger<String> stringExchanger = new Exchanger<>();

public static void main(String[] args) throws Exception {
    new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String str = stringExchanger.exchange("111的数据");
                    System.out.println("111获取到的数据: "+str);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String str = stringExchanger.exchange("222的数据");
                    System.out.println("222获取到的数据: "+str);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
          new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      semaphore.acquire();//申请资源
                      System.out.println(333);
                      semaphore.release();//释放资源
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }).start();
}

结果:

222获取到的数据: 111的数据
111获取到的数据: 222的数据

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值