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();
        }
    }
}
相关推荐
九圣残炎6 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge8 分钟前
Netty篇(入门编程)
java·linux·服务器
童先生10 分钟前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu12 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
Re.不晚35 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
老秦包你会38 分钟前
Qt第三课 ----------容器类控件
开发语言·qt
凤枭香40 分钟前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
雷神乐乐41 分钟前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven
ULTRA??44 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
码农派大星。1 小时前
Spring Boot 配置文件
java·spring boot·后端