【java】JUC中几个辅助类

CountDownLatch

适用场景: 一个线程需要等待其他多个线程执行完以后才执行

    CountDownLatch latch  = new CountDownLatch(2);
    Runnable r = ()->{
        System.out.println(Thread.currentThread().getId()+":exe task...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
             e.printStackTrace();
        }
        latch.countDown();

    };

    new Thread(r).start();
    Thread.sleep(2000); //第二个线程延迟启动
    new Thread(r).start();


    latch.await();

    System.out.println("all finished");

CyclicBarrier

适用场景:一批执行任务的线程 互相之间需要等待其他线程执行完任务才能一起继续下一个任务

    CyclicBarrier barrier = new CyclicBarrier(2);

    Runnable r = ()->{
        System.out.println(Thread.currentThread().getId()+":exe task...");
        try {
            Thread.sleep(2000);
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getId()+"exe next task...");
    };

    Thread t1 = new Thread(r);
    Thread t2 = new Thread(r);

    t1.start();

    Thread.sleep(3000);
    t2.start();

Semaphore

思想是 提供 n个许可,线程执行任务时每次都来获取许可,任务结束时归还许可,当许可不足时,线程阻塞直到有足够的许可

适用场景:当需要控制并行数量时

Semaphore semaphore = new Semaphore(2);
//Semaphore semaphore2 = new Semaphore(2,true);  // 第二个参数表示 是公平的(等待越久越优先)
Runnable r = ()-> {
    try {
        semaphore.acquire();    //获取许可 如果没有将阻塞 直到有
        //semaphore.acquire(2);  //可以一次获取多个 [许可]
        System.out.println(Thread.currentThread().getId()+": 拿到许可,执行任务...");
        Thread.sleep(2000);
        semaphore.release();   //释放[许可]
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
};

new Thread(r).start();
new Thread(r).start();
new Thread(r).start();        

娱乐部分

强行使用CountDownLatch实现CyclicBarrier的效果

   CountDownLatch latch  = new CountDownLatch(2);

    Runnable r = ()->{
        System.out.println(Thread.currentThread().getId()+":执行任务...");
        try {
            Thread.sleep(1000);     //模拟执行任务花费时间
            latch.countDown();
            latch.await();
            System.out.println(Thread.currentThread().getId()+":执行下一个任务...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };

    new Thread(r).start();
    Thread.sleep(2000);
    new Thread(r).start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值