Java并发编程九 CountDownLatch、CyclicBarrier、Semaphore

Java并发编程九 CountDownLatch、CyclicBarrier、Semaphore

1.CountDownLatch

CountDownLatch允许一个或多个线程等待其他线程完成操作。

2.CyclicBarrier

CyclicBarrier,让一组线程到达一个同步点后再一起继续运行,在其中任意一个线程未达到同步点,其他到达的线程均会被阻塞。

3.Semaphore

Semaphore是计数信号量。Semaphore管理一系列许可证。
每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;
每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。
然而,其实并没有实际的许可证这个对象,Semaphore只是维持了一个可获得许可证的数量。
Semaphore经常用于限制获取某种资源的线程数量。

package com.lyyz.sync.height.concurrent019;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * Created by lyyz on 18-5-17.
 */
public class CountCyclicSema_eg {

    public static void main(String[] args) {
        //CountDownLatch允许一个或多个线程等待其他线程完成操作。
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    countDownLatch.await();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread1");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    countDownLatch.await();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread2");
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    countDownLatch.countDown();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"thread3");
        Thread thread4 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    countDownLatch.countDown();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"thread4");

//        thread1.start();
//        thread2.start();
//        thread3.start();
//        thread4.start();
        //CyclicBarrier,让一组线程到达一个同步点后再一起继续运行,在其中任意一个线程未达到同步点,其他到达的线程均会被阻塞。
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
        Thread thread21 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        },"thread21");
        Thread thread22 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" 进入");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName()+" 完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        },"thread22");
//        thread22.start();
//        thread21.start();
        //Semaphore是计数信号量。Semaphore管理一系列许可证。
        // 每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;
        // 每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。
        // 然而,其实并没有实际的许可证这个对象,Semaphore只是维持了一个可获得许可证的数量。
        //Semaphore经常用于限制获取某种资源的线程数量。
        Semaphore semaphore = new Semaphore(5);

        for(int i=0;i<20;i++){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //获取许可
                        semaphore.acquire();
                        System.out.println(Thread.currentThread().getName()+"进入");
                        Thread.sleep(5000);
                        System.out.println(Thread.currentThread().getName()+"完成");
                        //释放许可
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"thread"+i);
            thread.start();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值