[Java] 统计从创建到结束所有线程消耗的总时间

JavaCode

public class CreateThread extends Thread{ CreateThread() {}@Overridepublic void run(){try{       // 每个线程睡眠5秒Thread.sleep(5000);}catch (InterruptedException e){e.printStackTrace();}}main(String[] args) throws InterruptedException{long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++){Thread thread = new CreateThread();thread.start();System.out.println(“activeCount : ” + Thread.activeCount());}System.out.println(“activeCountMain1 : ” + Thread.activeCount());// 每一个进程都有一个主线程,Java里面通常都指的是main方法//Thread.activeCount()==1时只有main线程在运行,其他的都结束或者挂起或者撤销了(true){if ( Thread.activeCount() == 1 ) break;}System.out.println(“activeCountMain2 : ” + Thread.activeCount());long end = System.currentTimeMillis();System.out.println(“Total Time: ” + (end – begin) + ” ms”);} // End function main()},家!甜蜜的家!天下最美好的莫过於家

[Java] 统计从创建到结束所有线程消耗的总时间

相关文章:

你感兴趣的文章:

标签云: