当前位置: 首页 > article >正文

CountDownlatch、CyclicBarrier、Semaphore使用介绍

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

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不同的是可以重复使用)

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(信号量,控制线程执行的数量)

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();
        }
    }
}


http://www.kler.cn/news/333552.html

相关文章:

  • 湖州自闭症寄宿学校:为孩子打造安全温馨的学习环境
  • SparkSQL-性能调优
  • 电脑失声,一招搞定
  • Bootstrap 4 导航栏:构建响应式和现代的网页导航
  • 【2006.07】UMLS工具——MetaMap原理深度解析
  • 21.1 k8s接口鉴权token认证和prometheus的实现
  • matlab 求绝对值
  • 1.资源《Arduino UNO R3 proteus 仿真工程》说明。
  • 在vscode在使用idea编辑器的快捷键
  • 【力扣 | SQL题 | 每日四题】力扣1440, 1378, 1421, 1393, 1407
  • MES系统实现制造业生产自动化、智能化与透明化
  • 计算机毕业设计 农场投入品运营管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 基于Springboot+Vue的计算中心共享平台(含源码数据库)
  • 引领5G驱动的全球数字营销革新:章鱼移动广告全球平台的崛起
  • 【ECMAScript 从入门到进阶教程】第二部分:中级概念(面向对象编程,异步编程,模块化,try/catch 语句)
  • 【AI知识点】二项分布(Binomial Distribution)
  • 数据结构--线性表(顺序结构)
  • C语言指针plus版练习
  • 【Linux】详解Linux下的工具(内含yum指令和vim指令)
  • 知识图谱入门——9: spaCy中命名实体识别(NER)任务中的预定义标签详解及案例(GPE、MONEY、PRODUCT、LAW、FAC、NORP是什么?)