java - How to calculate run-time for a multi-threaded program? -


i trying test performance (in terms of execution time) webcrawler having trouble timing due multi-threading taking place.

my main class:

class webcrawlertest {     //methods , variables etc     webcrawlertest(list<string> websites){     //     }      if(!started){         starttime = system.currenttimemillis();         executor = executors.newfixedthreadpool(32); //this value i'm tweaking         started=true;     }            for(string site : websites){         executor.submit(webprocessor = new allwebsiteprocessortest(site, deepsearch));     }                                    executor.shutdown();     //tried grabbing end time here no luck 

allwebsiteprocessortest class:

class allwebsiteprocessortest implements runnable{     //methods , var etc      allwebsiteprocessortest(string site, boolean deepsearch) {     }     public void run() {                  scansinglewebsite(websites);         for(string email:emails){             system.out.print(email + ", ");         }  private void scansinglewebsite(string website){     try {         string url = website;         document document = jsoup.connect(url).get();         grabemails(document.tostring());     }catch (exception e) {} 

with class (with main method), create instance of webcrawlertest , pass in array of websites. crawler works fine can't seem figure out how time it.

i can start time (system.getcurrenttime...();), problem end time. i've tried adding end time this:

//another class  public static void main(.....){     long start = getcurrent....();     webcrawlertest w = new webcrawlertest(listofsites, true);     long end = getcurrent....(); } 

which doesn't work. tried adding end after executor.shutdown(), again doesn't work (instantly triggered). how grab time final completed thread?

after shutting down executors pool

executor.shutdown(); //tried grabbing end time here no luck 

you can

executor.awaittermination(timeunit, value) 

this call block untill tasks completed. take time, subtract t0 , voila, have execution time.

shutdown() method assures no new tasks accepted excution queue. tasks in queue performed (shutdownnow() drops pending tasks). wait running tasks complete, have awaittermination().


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -