java多线程编程(4)CountDownLatch,CyclicBarrier,Semaphore的举例使用

前言

这3个组件具有相似的用途

CountDownLatch倒数锁

await()方法的线程会被挂起,它会等待直到count值为0才继续执行,也提供有超时时间的await()方法,当超过这个时间还在等待会抛出异常
countDown()将count值减1

import java.util.concurrent.CountDownLatch;

public class Test16 extends Thread{
	//如果把3改为4,可以看到3个线程都等待在那里了
    public static CountDownLatch countDownLatch = new CountDownLatch(3);
    public void run(){
        System.out.println("thread"+Thread.currentThread().getName()+"wait");	
        //countDownLatch-1,减少一个倒数
        countDownLatch.countDown();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("thread"+Thread.currentThread().getName()+"again");
    }
    public static void main(String[] args) {
        Test16 test1 = new Test16();
        Test16 test2 = new Test16();
        Test16 test3 = new Test16();
        test1.setName("test1");
        test2.setName("test2");
        test3.setName("test3");
        test1.start();
        test2.start();
        test3.start();
    }
}

CyclicBarrier循环屏障

类似于CountDownLatch的复用版本,可以重置屏障进行再次多个线程的等待
await()方法的线程告诉CyclicBarrier自己已经到达同步点,然后当前线程被阻塞。直到n(设定好)个参与线程调用了await方法,同样提供带超时时间的await和不带超时时间的await方法

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

public class Test17 extends Thread{
    public static CyclicBarrier cyclicBarrier = new CyclicBarrier (3);
    public void run(){
        System.out.println("thread"+Thread.currentThread().getName()+"start");
            try {
                cyclicBarrier.await();
            } catch (BrokenBarrierException | InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread"+Thread.currentThread().getName()+"again");
    }
    public static void main(String[] args) {
        Test17 test1 = new Test17();
        Test17 test2 = new Test17();
        Test17 test3 = new Test17();
        test1.setName("test1");
        test2.setName("test2");
        test3.setName("test3");
        test1.start();
        test2.start();
        test3.start();

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }
        //重置了屏障,又需要3次等待
        System.out.println("准备开始下一轮");
        cyclicBarrier.reset();

        Test17 test4 = new Test17();
        Test17 test5 = new Test17();
        Test17 test6 = new Test17();
        test4.setName("test4");
        test5.setName("test5");
        test6.setName("test6");
        test4.start();
        test5.start();
        test6.start();
    }
}

Semaphore信号量

void acquire():从此信号量获取一个许可,在提供一个许可前一直将线程阻塞,否则线程被中断。
void release():释放一个许可,将其返回给信号量。
int availablePermits():返回此信号量中当前可用的许可数。
boolean hasQueuedThreads():查询是否有线程正在等待获取。

import java.util.concurrent.Semaphore;

public class Test18 extends Thread{
    public static Semaphore semaphore = new Semaphore(3);
    public void run(){
        while (true){
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread"+Thread.currentThread().getName()+"acquire begin");
            System.out.println("thread"+Thread.currentThread().getName()+"release");
            semaphore.release();

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        Test18 test1 = new Test18();
        Test18 test2 = new Test18();
        Test18 test3 = new Test18();
        Test18 test4 = new Test18();
        Test18 test5 = new Test18();
        Test18 test6 = new Test18();
        test4.setName("test4");
        test5.setName("test5");
        test6.setName("test6");
        test1.setName("test1");
        test2.setName("test2");
        test3.setName("test3");
        test1.start();
        test2.start();
        test3.start();
        test4.start();
        test5.start();
        test6.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值