java多线程,主线程等待其他子线程执行完毕执行和多个线城一同执行

本文通过实例展示了Java并发编程中CyclicBarrier和CountDownLatch的使用。CyclicBarrier用于让一组线程等待其他线程到达某个屏障点,而CountDownLatch则允许一个线程等待多个线程完成执行。在示例中,主线程会等待多个子线程执行完毕后再继续执行。了解这两个工具可以帮助优化多线程程序的同步控制。
摘要由CSDN通过智能技术生成
package com.thread;

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

/**
 * 主线程等待其他线程执行完毕
 */
public class WhileTrueThread{

    public static void main(String[] args) throws Exception {
        WhileTrueThread.CyclicBarrierTest();
    }

    /**
     * 多个线城同时执行
     * CyclicBarrier.await() 达到 parties(本例子用3个线程)个线程后, parties个线程一同执行
     *  如果达不到将一直阻塞线程
     * @throws InterruptedException
     * @throws BrokenBarrierException
     */
    public static void CyclicBarrierTest() throws InterruptedException, BrokenBarrierException {
        CyclicBarrier cyclicBarrier=new CyclicBarrier(3);
        Thread thread1=new Thread(new ThreadClassCyclicBarrier(cyclicBarrier));
        Thread thread2=new Thread(new ThreadClassCyclicBarrier(cyclicBarrier));
        //Thread thread3=new Thread(new ThreadClassCyclicBarrier(cyclicBarrier));

        thread1.start();
        thread2.start();
        //thread3.start();
        //cyclicBarrier.await();
        System.out.println("主线程执行完毕");
    }

    /**
     * 主线程等待 多个线城执行完毕
     * CountDownLatch.await() CountDownLatch(3)中 3个线程都执行完毕才执行被await阻塞的主线程
     * @throws InterruptedException
     */
    public static void CountDownLatchTest() throws InterruptedException{
        CountDownLatch countDownLatch=new CountDownLatch(3);
        Thread thread1=new Thread(new ThreadClassCountDownLatch(countDownLatch));
        Thread thread2=new Thread(new ThreadClassCountDownLatch(countDownLatch));
        Thread thread3=new Thread(new ThreadClassCountDownLatch(countDownLatch));

        thread1.start();
        thread2.start();
        thread3.start();
        countDownLatch.await();
        System.out.println("主线程执行完毕");
    }


}
 class ThreadClassCountDownLatch implements Runnable {
     CountDownLatch countDownLatch = null;

     public ThreadClassCountDownLatch(CountDownLatch countDownLatch) {
         this.countDownLatch = countDownLatch;
     }

     @Override
     public void run() {
         System.out.println("ThreadClass 开始执行。。。");
         try {
             Thread.sleep(1000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println("ThreadClass 执行完毕  " + "Thread:" + Thread.currentThread().getId());
         countDownLatch.countDown();
     }
 }
 class ThreadClassCyclicBarrier implements Runnable {
     CyclicBarrier cyclicBarrier=null;
     public ThreadClassCyclicBarrier(CyclicBarrier cyclicBarrier){
         this.cyclicBarrier=cyclicBarrier;
     }
     @Override
     public void run(){
         System.out.println("ThreadClass 开始执行。。。");
         try {
             Thread.sleep(1000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }

         try {
             cyclicBarrier.await();
         } catch (InterruptedException e) {
             e.printStackTrace();
         } catch (BrokenBarrierException e) {
             e.printStackTrace();
         }
         System.out.println("ThreadClass 执行完毕  "+"Thread:"+Thread.currentThread().getId());
     }
}


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值