首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

监控线程结束解决思路

2012-05-21 
监控线程结束我有一个main方法,起了10个线程,我想在main方法中统计一下10个线程跑完后,总共用了多长时间。

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

[解决办法]
启动线程前,用System.currentTimeMillis()记录启动时间。

然后用join等待线程执行结束,然后再次调用System.currentTimeMillis(),并得到时间差。
[解决办法]

Java code
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();        }    }} 

热点排行