我有一个main方法,起了10个线程,我想在main方法中统计一下10个线程跑完后,总共用了多长时间。怎么弄??

解决方案 »

  1.   

    启动线程前,用System.currentTimeMillis()记录启动时间。然后用join等待线程执行结束,然后再次调用System.currentTimeMillis(),并得到时间差。
      

  2.   


    import java.text.SimpleDateFormat;
    import java.util.Date;public class C {
    private static int i = 0; synchronized static void increase() {
    if (i < 9) {
    i++;
    } else {
    C.class.notify();
    } } public static void main(String[] args) {
    synchronized (C.class) {
    long begin = System.currentTimeMillis();
    for (int i = 0; i <= 9; i++) {
    new Thread(new T()).start();
    }
    try {
    C.class.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(new SimpleDateFormat("ss秒SSS毫秒")
    .format(new Date(System.currentTimeMillis() - begin)));
    } } static class T implements Runnable { @Override
    public void run() {
    // dosomething C.increase(); } }
    }