CountDownlatch、CyclicBarrier、Semaphore使用介绍

一、CountDownlatch(多线程通信计数器实现多个线程的协同工作)

java 复制代码
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchTest {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CountDownLatch count = new CountDownLatch(3);
        executor.execute(() -> {
            try {
                findBy三方();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count.countDown();
        });

        executor.execute(() -> {
            try {
                findByMySQL();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count.countDown();
        });

        executor.execute(() -> {
            try {
                findBy服务();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count.countDown();
        });

        try {
            count.await();
            System.out.println("汇总结束");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void findBy三方() throws InterruptedException {
        Thread.sleep(700);
    }

    public static void findByMySQL() throws InterruptedException {
        Thread.sleep(200);
    }

    public static void findBy服务() throws InterruptedException {
        Thread.sleep(300);
    }
}

二、CyclicBarrier(与CountDownlatch不同的是可以重复使用)

java 复制代码
import java.util.concurrent.*;

public class CyclicBarrierTest {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
            System.out.println("汇总完毕");
        });
        executor.execute(() -> {
            try {
                findBy三方();
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }

        });

        executor.execute(() -> {
            try {
                findByMySQL();
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
        });

        executor.execute(() -> {
            try {
                findBy服务();
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
        });
        cyclicBarrier.reset();
    }

    public static void findBy三方() throws InterruptedException {
        Thread.sleep(700);
        System.out.println("查询完三方");
    }

    public static void findByMySQL() throws InterruptedException {
        Thread.sleep(200);
        System.out.println("查询完MySQL");
    }

    public static void findBy服务() throws InterruptedException {
        Thread.sleep(300);
        System.out.println("查询完b服务");
    }
}

三、Semaphore(信号量,控制线程执行的数量)

java 复制代码
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore sem = new Semaphore(2);
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try {
                    sem.acquire();
                    System.out.println(Thread.currentThread() + "I get it");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread() + "I release it");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    sem.release();
                }
            }).start();
        }
    }
}
相关推荐
y2508几秒前
《Object类》
java·开发语言
曙曙学编程1 分钟前
初级数据结构——树
android·java·数据结构
小技与小术5 分钟前
数据结构之树与二叉树
开发语言·数据结构·python
BestandW1shEs7 分钟前
彻底理解消息队列的作用及如何选择
java·kafka·rabbitmq·rocketmq
爱吃烤鸡翅的酸菜鱼9 分钟前
Java算法OJ(8)随机选择算法
java·数据结构·算法·排序算法
码蜂窝编程官方13 分钟前
【含开题报告+文档+PPT+源码】基于SpringBoot+Vue的虎鲸旅游攻略网的设计与实现
java·vue.js·spring boot·后端·spring·旅游
hccee26 分钟前
C# IO文件操作
开发语言·c#
Viktor_Ye29 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
hummhumm31 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
一二小选手35 分钟前
【Maven】IDEA创建Maven项目 Maven配置
java·maven