转自:http://blog.csdn.net/foart/article/details/8144315
- import java.util.concurrent.CountDownLatch;
- public class RRR {
- public static void main(String[] args) throws InterruptedException {
- int threadNumber = 10;
- for(int j =0;j<=2;j++){
- final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
- for (int i = 0; i < threadNumber; i++) {
- final int threadID = i;
- new Thread() {
- public void run() {
- try {
- Thread.sleep((long) (Math.random() * 10000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(String.format(
- "threadID:[%s] finished!!", threadID));
- countDownLatch.countDown();
- }
- }.start();
- }
- countDownLatch.await();
- System.out.println("main thread finished!!");
- System.out.println("还剩下"+countDownLatch.getCount() + "个子线程未执行");
- }
- }
- }