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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值