天天看点

concurrent包下的构件

CountDownLatch

用来同步一个或多个任务。

CountDownLatch

使用计数器来实现,通过构造器初始化计数器的初始数量,当计数器计数为0时,被

await()

阻塞的线程将继续执行

public CountDownLatch(int count)
           

主要使用方法

//调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
public void await() throws InterruptedException { }; 
//和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行
public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };  
//将count值减1
public void countDown() { };
           

示例(别处借来):

public class CountDownLatchTest {

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(2);
        System.out.println("主线程开始执行…… ……");
        //第一个子线程执行
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        es1.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("子线程:"+Thread.currentThread().getName()+"执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            }
        });
        es1.shutdown();

        //第二个子线程执行
        ExecutorService es2 = Executors.newSingleThreadExecutor();
        es2.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程:"+Thread.currentThread().getName()+"执行");
                latch.countDown();
            }
        });
        es2.shutdown();
        System.out.println("等待两个线程执行完毕…… ……");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("两个子线程都执行完毕,继续执行主线程");
    }
}
           

CountDownLatch

只能被触发一次,不能被重置。如果需要重置可使用

CyclicBarrier

CyclicBarrier

CountDownLatch

的可重置计数器版本。

不同点:

  • 可通过

    reset()

    方法去重置计数器
  • 可以向

    CylicBarrier

    提供一个“栅栏动作”,它是一个

    Runnable

    ,当计数器为0时,自动执行。
  • CountDownLatch

    是当计数器为0时,被阻塞的线程可继续执行代码,

    CyclicBarrier

    是当所有线程都到达

    await()

    (栅栏处)时,所有线程才可以继续执行代码,此时

    CyclicBarrier

    也会在

    nextGeneration()

    方法中重置计数器。

示例:

import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    import java.util.concurrent.*;
    public class Horse implements Runnable {

        private static int counter = 0;
        private final int id = counter++;
        private int strides = 0;
        private static Random random = new Random(47);
        private static CyclicBarrier barrier;
    
        public Horse(CyclicBarrier b) {
            barrier = b;
        }
    
        public synchronized int getStrides() {
            return strides;
        }
    
        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    synchronized (this) {
                        strides += random.nextInt(3);
                    }
                    barrier.await();
    
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    
        public String toString() {
            return "Horse" + id + " ";
        }
    
        public String tracks() {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < getStrides(); i++) {
                s.append("*");
            }
            s.append(id);
            return s.toString();
        }
    }
    
    class HorseRace {
        static final int FINISH_LINE = 75;
        private List<Horse> horses = new ArrayList<>();
        private ExecutorService exec = Executors.newCachedThreadPool();
        private CyclicBarrier barrier;
    
        public HorseRace(int nHorses, final int pause) {
            barrier = new CyclicBarrier(nHorses, new Runnable() {
                @Override
                public void run() {
                    StringBuilder s = new StringBuilder();
                    for (int i = 0; i < FINISH_LINE; i++) {
                        s.append("=");
                    }
                    System.out.println(s);
                    horses.forEach(temp -> System.out.println(temp.tracks()));
                    horses.stream().anyMatch(temp -> {
                        if (temp.getStrides() >= FINISH_LINE) {
                            System.out.println(temp + "won!");
                            exec.shutdownNow();
                            return true;
                        }
                        return false;
                    });
                    try {
                        TimeUnit.MILLISECONDS.sleep(pause);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            for (int i = 0; i < nHorses; i++) {
                Horse horse = new Horse(barrier);
                horses.add(horse);
                exec.execute(horse);
            }
        }
    
        public static void main(String[] args) {
            int nHorses = 7;
            int pause = 200;
            if (args.length > 0) {
                int n = new Integer(args[0]);
                nHorses = n > 0 ? n : nHorses;
            }
            if (args.length > 1) {
                int p = new Integer(args[1]);
                pause = p > -1 ? p : pause;
            }
            new HorseRace(nHorses,pause);
        }
    
    }
           

这是一个模拟的赛马游戏,开始所有马都会被阻塞,直到所有马都走了一步。同时

CyclicBarrier

重置计数器,所有马走了下一步后再次被阻塞。循环此类操作,直至有一匹马到达终点,中断所有线程。