Java c线程等待ab线程执行完再执行
1、LockSupport + AtomicInteger
- LockSupport.park() 函数表示挂起当前线程
- LockSupport.unpark© 函数表示解除线程c的阻塞状态
- AtomicInteger.decrementAndGet() 函数表示将该变量减一,并返回当前变量值(线程安全的原子类)
2、CountDownLatch
CountDownLatch latch = new CountDownLatch(2);
- latch.countDown() 函数将计数器减一
- latch.await() 函数表示,当计数器为0时才接着往下执行,否则陷入阻塞
总结:计数+阻塞。自己实现计数的话,要保证操作的原子性。
原连接:
【面试题】有线程A,B,C,C 要等A和B都执行完了才执行,怎么实现?_a线程等b和c执行完了再执行-CSDN博客