原文链接:https://blog.csdn.net/QQxiaoqiang1573/article/details/83008332
方法很少:
1、public CountDownLatch(int count)
构造函数,需传入计数的总数。
2、public void await()
阻塞线程,直到总计数值为0
3、public void countDown()
当前总计数减一
4、public long getCount()
当前总计数
下面是一个例子,可以使十个线程同时进行操作
final CountDownLatch latch = new CountDownLatch(1);
int threadCount = 10;
for (int i = 0; i < threadCount; i++) {
final int finalI = i;
new Thread() {
@Override
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "run: "+ finalI);
}
}.start();
}
latch.countDown();