方案(一)CountDownLatch:

使用CountDownLatch+Semaphore方式实现:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

public class TestABC {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(2);
        Semaphore semaphoreC = new Semaphore(1);
        
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(newjava.util.Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        }, "Thread-A");

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(newjava.util.Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        }, "Thread-B");

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {                
                try {
                    semaphoreC.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                semaphoreC.release();
            }
        }, "Thread-C");
        
        // 占用C锁,直到A/B线程完成后,才释放C锁。
        semaphoreC.acquire();
        
        threadA.start();
        threadB.start();
        threadC.start();
        
        countDownLatch.await(); 
        // 释放C锁,让C线程有获取锁的可能
        semaphoreC.release();
        
    }
}

上边使用CountDownLatch+Semaphore方式实现,但是缺点:上边这种方式会导致线程阻塞情况。下边这种方案是可以实现不阻塞线程的用法:

import java.util.concurrent.CountDownLatch;

public class TestABC {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(2);
        
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(new java.util.Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        }, "Thread-A");

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(new java.util.Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        }, "Thread-B");

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {    
                // 在C中等待A/B運算結束
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    throw new RuntimeException("CountDownLatch等待失败。。。",e);
                } 
                
                System.out.println(Thread.currentThread().getName());
            }
        }, "Thread-C");
        
        threadA.start();
        threadB.start();
        threadC.start();        
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-14
  • 2022-12-23
  • 2021-11-03
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2021-07-12
  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案