竞争无处不在,青春永不言败!专业撸代码,副业修bug

Talk is cheap , show me the code!



join 与 countdownlatch 的区别 扩展 栅栏 CyclicBarrier

我们先看一个 小例子 , 使用 join 与CountDownSlatch 都可以完成 当1,2线程 完全结束后 3 线程 start 对比我们就能够知道 CountDownSlatch 比 JOIN 更能够控制粒度 因为 他只要 计数 为0 就好

package com.ghc.mmall.concurrency.test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :Frank Li
 * @date :Created in 2019/7/18 10:30
 * @description:${description}
 * @modified By:
 * @version: $version$
 */
public class JoinTest {

    public static void main(String [] args){
        Thread work1Thread = new Thread(new Workder());
        work1Thread.setName("work1Thread");
        Thread work2Thread = new Thread(new Workder());
        work2Thread.setName("work2Thread");

        List<Thread> threads = new ArrayList<>();
        threads.add(work1Thread);
        threads.add(work2Thread);
        for(Thread thread:threads){
            thread.start();
        }
        for(Thread thread:threads){
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Thread work3Thread = new Thread(new Workder());
        work3Thread.setName("work3Thread");
        work3Thread.start();
//        System.out.println(Thread.currentThread().getName()+" ");


    }
}

class Workder implements Runnable{
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+"--> 开始工作");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"--> 第一阶段完成了");
        }catch(InterruptedException e){

        }

    }
}

output:
work1Thread--> 开始工作
work2Thread--> 开始工作
work2Thread--> 第一阶段完成了
work1Thread--> 第一阶段完成了
work3Thread--> 开始工作
work3Thread--> 第一阶段完成了

使用 CountDownSlatch 很显然 粒度会更小

package com.ghc.mmall.concurrency.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author :Frank Li
 * @date :Created in 2019/7/18 11:30
 * @description:${description}
 * @modified By:
 * @version: $version$
 */
public class CountDownSlatchTest {
    private static final CountDownLatch countDownLatch = new CountDownLatch(2);
    public static void main(String [] args){
        Thread worker1 = new Thread(new Worker(countDownLatch),"worker1");
        Thread worker2 = new Thread(new Worker(countDownLatch), "worker2");
        Thread worker3 = new Thread(new Worker(countDownLatch), "worker3");
        List<Thread> threads = new ArrayList<>(3);
        threads.add(worker1);
        threads.add(worker2);
        threads.add(worker3);
        for(Thread thread:threads){
            thread.start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(Thread thread:threads){
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class Worker implements Runnable{
    private CountDownLatch countDownLatch;

    Worker(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName()+"--> 开始第一阶段任务...");
        try{
            Thread.sleep(2000);
            countDownLatch.countDown();
            System.out.println(Thread.currentThread().getName()+"--> 结束第一阶段任务...");
        }catch (InterruptedException e){

        }

        try {
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName()+"--> 开始第二阶段任务...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"--> 结束第二阶段任务...");
    }

}

output:

worker1--> 开始第一阶段任务...
worker2--> 开始第一阶段任务...
worker3--> 开始第一阶段任务...
worker1--> 结束第一阶段任务...
worker2--> 结束第一阶段任务...
worker3--> 结束第一阶段任务...
worker1--> 开始第二阶段任务...
worker1--> 结束第二阶段任务...
worker2--> 开始第二阶段任务...
worker2--> 结束第二阶段任务...
worker3--> 开始第二阶段任务...
worker3--> 结束第二阶段任务...

总结


posted @ 2019-07-18 11:29  云雾散人  阅读(230)  评论(0编辑  收藏  举报

Your attitude not your aptitude will determine your altitude!

如果有来生,一个人去远行,看不同的风景,感受生命的活力!