zoukankan      html  css  js  c++  java
  • 转:java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例

    java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例

    1、CountDownLatch:一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。

    2、ThreadPoolExecutor/ExecutorService:线程池,使用线程池可以复用线程,降低频繁创建线程造成的性能消耗,同时对线程的创建、启动、停止、销毁等操作更简便。

    3、使用场景举例:
    年末公司组织团建,要求每一位员工周六上午8点到公司门口集合,统一乘坐公司所租大巴前往目的地。
    在这个案例中,公司作为主线程,员工作为子线程。

    4、代码示例:

    package com.test.thread;

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    /**
    * @author javaloveiphone
    * @date 创建时间:2017年1月25日 上午10:59:11
    * @Description:
    */
    public class Company {
    public static void main(String[] args) throws InterruptedException {

    //员工数量
    int count = 5;
    //创建计数器
    //构造参数传入的数量值代表的是latch.countDown()调用的次数
    CountDownLatch latch = new CountDownLatch(count);

    //创建线程池,可以通过以下方式创建
    //ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1,1,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(count));
    ExecutorService threadPool = Executors.newFixedThreadPool(count);

    System.out.println("公司发送通知,每一位员工在周六早上8点到公司大门口集合");
    for(int i =0;i<count ;i++){
    //将子线程添加进线程池执行
    Thread.sleep(10);
    threadPool.execute(new Employee(latch,i+1));
    }
    try {
    //阻塞当前线程,直到所有员工到达公司大门口之后才执行
    latch.await();
    // 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。
    //latch.await(long timeout, TimeUnit unit)
    System.out.println("所有员工已经到达公司大门口,大巴车发动,前往活动目的地。");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }finally{
    //最后关闭线程池,但执行以前提交的任务,不接受新任务
    threadPool.shutdown();
    //关闭线程池,停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。
    //threadPool.shutdownNow();
    }
    }
    }

    //分布式工作线程
    class Employee implements Runnable{

    private CountDownLatch latch;
    private int employeeIndex;

    public Employee(CountDownLatch latch,int employeeIndex){
    this.latch = latch;
    this.employeeIndex = employeeIndex;
    }

    @Override
    public void run() {
    try {
    System.out.println("员工:"+employeeIndex+",正在前往公司大门口集合...");
    Thread.sleep(10);
    System.out.println("员工:"+employeeIndex+",已到达。");
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    //当前计算工作已结束,计数器减一
    latch.countDown();
    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //执行coutDown()之后,继续执行自己的工作,不受主线程的影响
    System.out.println("员工:"+employeeIndex+",吃饭、喝水、拍照。");
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    打印输出结果如下:

    公司发送通知,每一位员工在周六早上8点到公司大门口集合
    员工:1,正在前往公司大门口集合...
    员工:1,已到达。
    员工:2,正在前往公司大门口集合...
    员工:2,已到达。
    员工:1,吃饭、喝水、拍照。
    员工:3,正在前往公司大门口集合...
    员工:2,吃饭、喝水、拍照。
    员工:3,已到达。
    员工:4,正在前往公司大门口集合...
    员工:3,吃饭、喝水、拍照。
    员工:4,已到达。
    员工:5,正在前往公司大门口集合...
    员工:4,吃饭、喝水、拍照。
    员工:5,已到达。
    所有员工已经到达公司大门口,大巴车发动,前往活动目的地。
    员工:5,吃饭、喝水、拍照。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    注意:
    每一个员工到达之后,执行countDown()方法,直到所有员工到达之后,计数器为0,主线程才会继续执行。

    但子线程执行了countDown()方法,之后会继续自己的工作,比如上面的【吃饭、喝水、拍照】,是不受主线程是否阻塞、其它线程是否已经执行countDown()方法的影响的。

    5、CountDownLatch与CyclicBarrier的对比可以看: java多线程CyclicBarrier使用示例,让线程起步走

    6、参考:
    http://www.importnew.com/15731.html
    http://www.cnblogs.com/yezhenhan/archive/2012/01/07/2315652.html
    ---------------------
    作者:逍遥不羁
    来源:CSDN
    原文:https://blog.csdn.net/javaloveiphone/article/details/54729821
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    tomcat中配置jmx监控
    常用sql
    String、StringBuffer、StringBuilder的不同使用场景
    求交集的几种方法
    使用liunx部署的心得
    几种有助于开发的注释方式。
    SpringDataJPA的几个使用记录
    今年要完成的几件事
    研究kisso跨域登录的心得
    SpringBoot使用的心得记录
  • 原文地址:https://www.cnblogs.com/kira2will/p/10486529.html
Copyright © 2011-2022 走看看