- import java.util.*;
- import java.util.concurrent.CountDownLatch;
- public class Test {
- public static void main(String[] args) {
- long startTime= System.currentTimeMillis();//开始时间
- int threadNumber = 2;
- final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
- new Thread(new Runnable(){
- public void run(){
- for(int i=0;i<1000;i++){
- System.out.println("A:"+i);
- }
- countDownLatch.countDown();
- }
- }).start();
- new Thread(new Runnable(){
- public void run() {
- for(int j=0;j<1000;j++){
- System.out.println("B:"+j);
- }
- countDownLatch.countDown();
- }
- }).start();
- try {
- countDownLatch.await();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("执行总时间:"+(System.currentTimeMillis()-startTime));
- }
- }
主线程统计子线程执行时间---countDownLatch
最新推荐文章于 2021-02-25 05:56:59 发布