前序:
上周测试给开发的同事所开发的模块提出了一个bug,并且还是偶现。
经过仔细查看代码,发现是在业务中启用了多线程,2个线程同时跑,但是新启动的2个线程必须保证一个完成之后另一个再继续运行,才能消除bug。
什么时候用?
多线程是在很多地方都会用到的,但是我们如果想要实现在某个特定的线程运行完之后,再启动另外一个线程呢,这个时候CountDownLatch就可以派上用场了
怎么用?
先看看普通的多线程代码:
1 package code;
2
3 public class MyThread extends Thread {
4 public static void main(String[] args) {
5 MyThread th = new MyThread();
6 Thread t1 = new Thread(th, "Mythread");
7 t1.start();
8 System.out.println(Thread.currentThread().getName());
9 }
10 public void run()
11 {
12 Mythread1 th2 = new Mythread1();
13 Thread t2 = new Thread(th2, "Mythread1");
14 t2.start();
15 System.out.println(this.currentThread().getName());
16 }
17 class Mythread1 extends Thread
18 {
19 public void run() {
20 try {
21 Thread.sleep(1000);
22 } catch (InterruptedException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26 System.out.println(this.currentThread().getName());
27 }
28
29 }
30 }
代码如上,先用MyThread继承了Thread类,然后在MyThread类内部又写了一个MyThread1类,同样也是继承了Thread类,并且在run方法里面让它睡1秒,这样运行代码,就会打印出:

从上面的输出顺序可以看出,先是启动了main线程,然后再启动了MyThread线程,在MyThread线程中,又启动了MyThread1线程。但是由于让MyThread1线程睡了1秒,模拟处理后续业务,这样他就比MyThread运行完毕的时间晚一些。
现在,在代码中加上CountDownLatch ,要让MyThread1先运行完毕,再让MyThread继续运行。
1 package code;
2
3 import java.util.concurrent.CountDownLatch;
4
5 public class MyThread extends Thread {
6 CountDownLatch countDownLatch = new CountDownLatch(1);
7 public static void main(String[] args) {
8 MyThread th = new MyThread();
9 Thread t1 = new Thread(th, "Mythread");
10 t1.start();
11 System.out.println(Thread.currentThread().getName());
12 }
13 public void run()
14 {
15 Mythread1 th2 = new Mythread1();
16 Thread t2 = new Thread(th2, "Mythread1");
17 t2.start();
18 try {
19 countDownLatch.await();
20 } catch (InterruptedException e) {
21 e.printStackTrace();
22 }
23 System.out.println(this.currentThread().getName());
24 }
25 class Mythread1 extends Thread
26 {
27 public void run() {
28 try {
29 Thread.sleep(1000);
30 } catch (InterruptedException e) {
31 e.printStackTrace();
32 }
33 System.out.println(this.currentThread().getName());
34 countDownLatch.countDown();
35 }
36
37 }
38 }
代码写法如上所示,大致分三步
1,我们先new一个CountDownLatch对象入参设置为1(我个人理解的这个就像是new一个数组一样,什么时候数组清空了,那就可以让被中断的线程继续运行了)
2,在MyThread类中调用countDownLatch.await();让当前线程停止运行。
3,在Mythread1类中调用countDownLatch.countDown()方法。当Mythread1全部执行完毕,再最后调用该方法,作用就是把我说的“数组”清空。
看看输出的打印结果
结果如上图,是符合预期的结果的。
最后再说下CountDownLatch countDownLatch = new CountDownLatch(1)的入参,这块设置的是1,那就需要调用一次countDownLatch.countDown()减去1。
如果是其他数字,那就要调用相应的次数,否则调用countDownLatch.await()的线程都不会被继续执行。
give me the ball!