Java 如何计算多线程程序的运行时?

Java 如何计算多线程程序的运行时?,java,multithreading,Java,Multithreading,我正在尝试测试webcrawler的性能(在执行时间方面),但由于发生了多线程,我在计时方面遇到了问题 我的主课: class WebCrawlerTest { //methods and variables etc WebCrawlerTest(List<String> websites){ // } if(!started){ startTime = System.currentTimeMillis();

我正在尝试测试webcrawler的性能(在执行时间方面),但由于发生了多线程,我在计时方面遇到了问题

我的主课:

class WebCrawlerTest {
    //methods and variables etc
    WebCrawlerTest(List<String> websites){
    //
    }

    if(!started){
        startTime = System.currentTimeMillis();
        executor = Executors.newFixedThreadPool(32); //this is the value I'm tweaking
        started=true;
    }       
    for(String site : websites){
        executor.submit(webProcessor = new AllWebsiteProcessorTest(site, deepSearch));
    }                               
    executor.shutdown();
    //tried grabbing end time here with no luck
使用另一个类(使用
main
方法),我创建了
WebCrawlerTest
的一个实例,然后传入一系列网站。爬行器工作正常,但我似乎不知道如何计时

我可以获取开始时间(
System.getCurrentTime…();
),但问题是结束时间。我尝试过这样添加结束时间:

//another class 
public static void main(.....){
    long start = getCurrent....();
    WebCrawlerTest w = new WebCrawlerTest(listOfSites, true);
    long end = getCurrent....();
}

这不管用。我还尝试在executor.shutdown()之后添加
end
,但仍然无法工作(立即触发)。如何抓紧时间完成最终完成的线程

关闭执行者池后

executor.shutdown();
//tried grabbing end time here with no luck
你可以简单地

executor.awaitTermination(TimeUnit, value)
此调用将被阻止,直到所有任务完成。拿时间,从中减去T0,瞧,我们有执行时间


shutdown()
方法只确保执行队列中不会接受任何新任务。将执行队列中已存在的任务(
shutdownNow()
drops挂起的任务)。要等待所有当前正在运行的任务完成,您必须
waittermination()

您可以将
Runnable
更改为
Callable
,返回的结果是完成的时间戳,然后通过
Future#get()
聚合这些时间戳,这正是我要查找的,谢谢您提供的额外信息!:-)!
shutdownNow()
是我在不久的将来肯定需要的另一种方法!我很高兴能帮上忙。
executor.awaitTermination(TimeUnit, value)