延迟线程的进度,等待闭锁条件的结束
import java.io.*; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import java.util.concurrent.CountDownLatch; public class Main { final static CountDownLatch start = new CountDownLatch(1); final static CountDownLatch end = new CountDownLatch(5); public static void main(String[] args) throws FileNotFoundException, NoSuchAlgorithmException, ParseException, InterruptedException { long t1 = System.currentTimeMillis(); for (int i = 0; i < 5; i++) { new Thread(new MyRunable(i)).start(); } start.countDown(); end.await(); // long total = Integer.MAX_VALUE*5; // for(long i = 0; i < total;i++); long t2 = System.currentTimeMillis(); System.out.println("total time=" + (t2 - t1)); System.out.println("total time=" + (t2 - t1) / 1000); } static class MyRunable implements Runnable { int i; public MyRunable(int i) { this.i = i; } public void run() { try { start.await(); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < Integer.MAX_VALUE; i++) if (i == 100) { System.out.println(i + " is 100"); } System.out.println(i + " is end"); end.countDown(); } } }