How to calculate runtime for a multi-threaded program?

I am trying to test the performance (in terms of runtime) for my web browser, but I am having trouble choosing due to multithreading.

My main class:

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 

Class AllWebsiteProcessorTest:

 class AllWebsiteProcessorTest implements Runnable{ //methods and 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 another class (with the main method) I create an instance of WebCrawlerTest and then pass in an array of websites. The scanner works fine, but I can’t figure out how to do this.

I can get the start time ( System.getCurrentTime...(); ), but the problem is the end time. I tried adding end time as follows:

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

What does not work. I also tried adding end after executor.shutdown() , which again does not work (instantly starts). How to capture time for the final completed stream ?

+5
source share
1 answer

After closing the pool of performers

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

You can just

 executor.awaitTermination(TimeUnit, value) 

This call will be blocked until all tasks are completed. Take the time, subtract T0 from it and voila, we have the lead time.

shutdown() simply ensures that new tasks are not queued for exception. Jobs already in the queue will be executed ( shutdownNow() discards pending tasks). To wait for the completion of all currently running tasks, you must awaitTermination() .

+5
source

Source: https://habr.com/ru/post/1216592/


All Articles