[Java] CountDownLatch和ExecutorService的简单使用

[Java] CountDownLatch和ExecutorService的简单使用

一、前言

  • 环境说明:

JDK:1.8

官方API文档:https://docs.oracle.com/javase/8/docs/api/index.html

参考:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html

二、记录

场景说明:

1.模拟多个动物,同时喂食,并记录过程

  • 动物接口
package com.demo.thread;

/**
 * 动物接口
 * */
public interface IAnimal {

    /**
     * 喂食
     * */
    public String feeding() throws InterruptedException;

}
  • 动物类:狗
package com.demo.thread;

/**
 * 狗
 * */
public class Dog implements IAnimal {
    @Override
    public String feeding() throws InterruptedException {
        Thread.sleep(3000); // 模拟耗时
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("准备美味的骨头");
        stringBuffer.append(" > 开始喂食");
        return stringBuffer.toString();
    }
}
  • 动物类:猫
package com.demo.thread;

/**
 * 猫
 * */
public class Cat implements IAnimal {
    @Override
    public String feeding() throws InterruptedException {
        Thread.sleep(5000); // 模拟耗时
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("准备美味的小鱼");
        stringBuffer.append(" > 开始喂食");
        return stringBuffer.toString();
    }
}
  • 任务类:喂食
package com.demo.thread;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * 任务类
 * */
public class FeedingTask implements Runnable {

    private String id;
    private IAnimal iAnimal;
    private CountDownLatch countDownLatch;
    private Map<String, String> map; // 结果

    // constructor
    public FeedingTask(String id, IAnimal iAnimal
                       , CountDownLatch countDownLatch, Map<String, String> map){
        this.id = id;
        this.iAnimal = iAnimal;
        this.countDownLatch = countDownLatch;
        this.map = map;
    }

    @Override
    public void run() {
        try {
            long start = System.currentTimeMillis(); // 开始时间戳

            String flow = iAnimal.feeding(); // 开始喂食
            map.put(id, flow); // 记录过程

            long end = System.currentTimeMillis(); // 结束时间戳
            System.out.println(id + " exec="+ (end - start) +"ms");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            countDownLatch.countDown(); // 计数器递减
        }
    }
}
  • 主入口
package com.demo.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainDemo {

    public static void main(String[] args) throws InterruptedException {
        Map<String, String> map = new HashMap<>();
        CountDownLatch countDownLatch = new CountDownLatch(2); // M 个喂食任务
        ExecutorService executorService 
            = Executors.newFixedThreadPool(2); // N 个任务同时执行(线程池)
        // Dog Feeding
        executorService.execute(new FeedingTask("DOG", new Dog()
                                                , countDownLatch, map));
        // Cat Feeding
        executorService.execute(new FeedingTask("CAT", new Cat()
                                                , countDownLatch, map));

        // 等待所有动物喂食完成(等待计数器归零)
        countDownLatch.await();
        executorService.shutdown(); // 关闭执行线程池

        // 输出结果
        System.out.println(map.toString());
    }
}
  • 输出结果
DOG exec=3000ms
CAT exec=5000ms
{CAT=准备美味的小鱼 > 开始喂食, DOG=准备美味的骨头 > 开始喂食}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趴着喝可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值