---
title: "java并发编程"
author: "wangmuchang"
date: "2016-01-20T18:17:31Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2016-01-20T18:17:31Z"
id: 2272293
url: "https://wangmuchang.iteye.com/blog/2272293"
---

## 一.CountDownLatch用法

CountDownLatch类位于java.util.concurrent包下，利用它可以实现类似计数器的功能。比如有一个任务A，它要等待其他4个任务执行完毕之后才能执行，此时就可以利用CountDownLatch来实现这种功能了。

```
public class Test {

     public static void main(String[] args) {    

         final CountDownLatch latch = new CountDownLatch(2);

          

         new Thread(){

             public void run() {

                 try {

                     System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");

                    Thread.sleep(3000);

                    System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");

                    latch.countDown();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

             };

         }.start();

          

         new Thread(){

             public void run() {

                 try {

                     System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");

                     Thread.sleep(3000);

                     System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");

                     latch.countDown();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

             };

         }.start();

          

         try {

             System.out.println("等待2个子线程执行完毕...");

            latch.await();

            System.out.println("2个子线程已经执行完毕");

            System.out.println("继续执行主线程");

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

     } 

}
```

 

 

## 二.CyclicBarrier用法

字面意思回环栅栏，通过它可以实现让一组线程等待至某个状态之后再全部同时执行。叫做回环是因为当所有等待线程都被释放以后，CyclicBarrier可以被重用。我们暂且把这个状态就叫做barrier，当调用await()方法之后，线程就处于barrier了。

```
public class Test {

    public static void main(String[] args) {

        int N = 4;

        CyclicBarrier barrier  = new CyclicBarrier(N);

        for(int i=0;i<N;i++)

            new Writer(barrier).start();

    } 

    static class Writer extends Thread{

        private CyclicBarrier cyclicBarrier;

        public Writer(CyclicBarrier cyclicBarrier) {

            this.cyclicBarrier = cyclicBarrier;

        }

 

        @Override

        public void run() {

            System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据...");

            try {

                Thread.sleep(5000);      //以睡眠来模拟写入数据操作

                System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕，等待其他线程写入完毕");

                cyclicBarrier.await();

            } catch (InterruptedException e) {

                e.printStackTrace();

            }catch(BrokenBarrierException e){

                e.printStackTrace();

            }

            System.out.println("所有线程写入完毕，继续处理其他任务...");

        }

    }

}
```