0

I am trying to run a Selenium Webdriver MultiThreaded Test using WebDriverManager ( Reference was taken -> https://www.programcreek.com/java-api-examples/?api=io.github.bonigarcia.wdm.WebDriverManager

@Test 
public void test() throws InterruptedException {
    private int NUM_DATA =2;
    private int NUM_LATCH=2;
    private int POOL_SIZE=2;

    CountDownLatch latch = new CountDownLatch(NUM_LATCH);
    ExecutorService executorService = newFixedThreadPool(POOL_SIZE);
   
    for (int i = 0; i < NUM_DATA; i++) {
        executorService.submit(() -> {
            try {
                WebDriverManager.chromedriver().setup();
                ChromeOptions options = new ChromeOptions();
                options.addArguments("--headless");
                WebDriver driver = new ChromeDriver(options);
                driver.get(
                        "https://bonigarcia.github.io/selenium-jupiter/");
                String title = driver.getTitle();
                System.out.println(title);
                driver.quit();
            } finally {
                latch.countDown();
            }
        });
    }

    latch.await();
    executorService.shutdown(); }

In my case

  1. NUM_LATCH = POOL_SIZE = NUM_DATA == > WORKING FINE
  2. NUM_LATCH = 2 = POOL_SIZE , NUM_DATA =6 = > NOT ALL INSTANCES ARE CLOSED FOR TRHEAD
  3. NUM_LATCH = NUM_DATA = 6 , POOL_SIZE = 2 => WORKING FINE

BY working fine means here means , all the browsers instances are opened and execution happens and then all browser are closed.

My problem statement is if i have 1000 set of NUM_DATA and POOL_SIZE is 10 at a time . do i have to set NUM_LATCH always equal to NUM_DATA ?

As per the article in "https://www.baeldung.com/java-countdown-latch"

"If we were doing some parallel processing, we could instantiate the CountDownLatch with the same value for the counter as a number of threads we want to work across"

but here we are not setting NUM_LATCH = POOL_SIZE but rather number of num_Latch= Num_Data_Set . Is this correct way to use CountDownLatch ??

Karan
  • 443
  • 5
  • 14

1 Answers1

1

do i have to set NUM_LATCH always equal to NUM_DATA?

How would it make any sense otherwise? What is the purpose of the latch? NUM_DATA is the number of tasks that you submit to the executor. I presume that you want the latch.await() call in the main thread to return when all of those tasks have completed. Is that right? If that's what you want, then the initial value of the latch had better be equal to NUM_DATA.

I think you should not declare NUM_LATCH at all, and simply write new CountDownLatch(NUM_DATA). That makes it clear to anybody else who is reading your code that you need the initial value of the latch to be equal to the number of tasks.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • yes, agree totally with you. i need to make sure that my Data set is very large ( 100k) does making CountDownLatch to that number make any significant impact ?? I was in the notion that CountDownLatch should be equal to the number of Threads we are setting in Threadpool i.e. 10 in my example above – Karan Nov 20 '22 at 16:54
  • 1
    @Karan, Re, "...100k..." There is no _technical_ reason why you could not do that with CountDownLatch, but with CountDownLatch, you need to know exactly how many data will be processed before you begin to process them. That does not sound like a robust way of programming. Real-world data processing applications are more likely to read and process data until there are no more to be processed, and do it without needing to know before hand how many there will be. I would consider maybe thinking up some other way of sending an "all done" message to the main thread. – Solomon Slow Nov 20 '22 at 17:35