如何统计每个线程单独运行的时间呢?
比如同时起10个线程,有办法得到每个线程从开始到结束的耗时吗?
public class TestThread {
public static void startThread() {
new Thread() {
public void run() {
System.out.println(this.currentThread().getName()+"开始时间[" + new java.util.Date().getTime());
try {
Thread.sleep(2000);
}catch(Exception ex) {
ex.printStackTrace();
}
System.out.println(this.currentThread().getName()+"结束时间[" + new java.util.Date().getTime());
}
}.start();
}
public static void main(String[] args) {
TestThread.startThread();
TestThread.startThread();
TestThread.startThread();
TestThread.startThread();
}
}
CountDownLatch 可以实现 具体怎么做 我忘了 楼主可以研究下
在线程的开始和结束前面加上时间统计
楼主可以试试:
public void run(){
Thread.currentThead().getName() + “ start time-->”+System.currentTimemillis();
//业务逻辑
Thread.currentThead().getName() + “ end time-->”+System.currentTimemillis();
}