多线程设计模式-Count Down设计模式

Count Down设计模式

jdk自带的阻塞主线程模式,类似于join的功能:通过统计执行线程的个数,来判断执行线程是否执行完成,主线程阻塞

package com.ln.concurrent.chapter14;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter14
 * @Name:JDKCountDown
 * @Author:linianest
 * @CreateTime:2021/1/4 9:12
 * @version:1.0
 * @Description TODO: jdk自带的阻塞主线程模式,类似于join的功能
 */

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

/**
 * 通过统计执行线程的个数,来判断执行线程是否执行完成,主线程阻塞
 */
public class JDKCountDown {
    private static final Random random = new Random(System.currentTimeMillis());


    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(5);
        System.out.println("多线程执行任务");

        IntStream.rangeClosed(1, 5).forEach(i ->
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " is working.");
                try {
                    Thread.sleep(random.nextInt(1_000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 统计当前线程执行完成的个数
                latch.countDown();
            }, String.valueOf(i)).start()
        );
        // 主线程阻塞
        latch.await();
        System.out.println("多线程执行任务全部结束,准备第二阶段任务");
        System.out.println("......................");
        System.out.println("FINISH.");
    }
}

自定义一个CountDown

package com.ln.concurrent.chapter14;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter14
 * @Name:CountDown
 * @Author:linianest
 * @CreateTime:2021/1/4 9:28
 * @version:1.0
 * @Description TODO: 自定义计数器
 */
public class CountDown {
    private final int total;
    private int counter;

    public CountDown(int total) {
        this.total = total;
    }
    public void down(){
        synchronized (this){
            this.counter++;
            this.notifyAll();
        }
    }

    public void await(){
        synchronized (this){
            while (counter!=total){
                this.await();
            }
        }
    }
}

测试

package com.ln.concurrent.chapter14;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter14
 * @Name:CustomCountDownClient
 * @Author:linianest
 * @CreateTime:2021/1/4 11:04
 * @version:1.0
 * @Description TODO: 测试
 */
public class CustomCountDownClient {
    private static final Random random = new Random(System.currentTimeMillis());


    public static void main(String[] args) throws InterruptedException {
        final CountDown latch = new CountDown(5);
        System.out.println("多线程执行任务");

        IntStream.rangeClosed(1, 5).forEach(i ->
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + " is working.");
                    try {
                        Thread.sleep(random.nextInt(1_000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 统计当前线程执行完成的个数
                    latch.down();
                }, String.valueOf(i)).start()
        );
        // 主线程阻塞
        latch.await();
        System.out.println("多线程执行任务全部结束,准备第二阶段任务");
        System.out.println("......................");
        System.out.println("FINISH.");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值