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();
        }
    }
}
相关推荐
何政@几秒前
如何快速自定义一个Spring Boot Starter!!
java·spring boot·spring·自定义配置·springboot自动配置·快速构建一个starter·
喝旺仔la12 分钟前
Python与MongoDB交互
开发语言·python·mongodb
MavenTalk13 分钟前
Python批量处理客户明细表格数据,挖掘更大价值
开发语言·python·表格处理
Web项目开发19 分钟前
JAVA JDK华为云镜像下载,速度很快
java
夜色呦22 分钟前
利用Spring Boot构建足球青训管理平台
java·spring boot·后端
计算机专业源码23 分钟前
springboot儿童物品共享平台的设计与实现
java·spring boot·后端
尘浮生23 分钟前
Java项目实战II基于Java+Spring Boot+MySQL的购物推荐网站的设计与实现(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·maven·intellij-idea
2402_8575893627 分钟前
Spring Boot框架下的足球青训俱乐部后台开发
java·spring boot·后端
2401_8576363928 分钟前
足球青训后台管理系统:Spring Boot实现指南
java·spring boot·后端
杨哥带你写代码30 分钟前
Spring Boot技术在足球青训管理中的实践与挑战
java·spring boot·后端