Java 线程 - CountDownLatch / CyclicBarrier

  • CountDownLatch 一般用于某个线程A等待若干个其他线程执行完任务之后,它才执行
package practice;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class CountDonwLatchTest {

    static CountDownLatch countDownLatch = new CountDownLatch(3);
    static int[][] panel = new int[3][10];

    public static void main(String[] args) {
        Thread thread0 = new Thread(new CountDonwLatchTest.DrawLine(0));
        Thread thread1 = new Thread(new CountDonwLatchTest.DrawLine(1));
        Thread thread2 = new Thread(new CountDonwLatchTest.DrawLine(2));
        Thread showThread = new Thread(new CountDonwLatchTest.ShowPanel());

        showThread.start();
        thread0.start();
        thread1.start();
        thread2.start();
    }

    static class DrawLine implements Runnable {

        int index;

        public DrawLine(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                panel[index][i] = index * 10 + i;
                try {
                    Thread.sleep(20);
                    System.out.println(index + "," + i + " done");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            countDownLatch.countDown();// 将count值减1
        }

    }

    static class ShowPanel implements Runnable {

        @Override
        public void run() {
            try {
                countDownLatch.await(); // 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < panel.length; i++) {
                for (int j = 0; j < panel[i].length; j++) {
                    System.out.print(panel[i][j] + "\t,");
                }
                System.out.println();
            }

        }

    }
}

  • CyclicBarrier 它可以实现让一组线程等待至某个状态之后再全部同时执行
package practice;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

    static CyclicBarrier barrier = new CyclicBarrier(3,new Mom()); 
    
    public static void main(String[] args) {
        Thread c1 = new Thread(new Guy("Tom"));
        Thread c2 = new Thread(new Guy("Lily"));
        Thread c3 = new Thread(new Guy("Jerry"));
        
        c1.start();
        c2.start();
        c3.start();
    }
    

    static class Guy implements Runnable{

        private String name;
        
        public Guy(String name) {
            this.name = name;
        }
        
        @Override
        public void run() {
            System.out.println(name +" is eating ...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name +" has done. I'm waiting to play.");
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(name +" joined.");
        }
        
    }

    static class Mom implements Runnable{

        @Override
        public void run() {
            System.out.println("Mom clean the table.");
        }
        
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容