giograno
giograno

Reputation: 1809

Run 1 thread at time with ExecutorService

I'm developing a review extractor for the Play Store, using Selenium. My piece of code is the following:

public void extract() {

    ExecutorService executor = Executors.newFixedThreadPool(this.configurationManager.getNumberOfThreadToUse());

    for (String currentApp : appsToMine) {
        ArrayList<String> aux = new ArrayList<>();
        aux.add(currentApp);
        Crawler googlePlayStoreCrawler = CrawlerFactory.getCrawler(this.configurationManager, aux, "google");
        executor.execute(googlePlayStoreCrawler);
    }
    executor.shutdown();
}

Here, Crawler implements Runnable. Using more than on thread, where each one open a separated instance of Firefox, Selenium fails because the web page is not visibile (it is hidden by a new windows opened by the another thread). So, I'm trying to execute all the process with only 1 thread at time.

But also if I instantiate an ExecutorService using newFixedThreadPool with 1 as parameter, a new threads always starts when the previous is running.

Upvotes: 1

Views: 685

Answers (1)

aviad
aviad

Reputation: 1573

Sounds like you don't need to use threads at all. Just run the crawler via the main thread:

for (String currentApp : appsToMine) {
    ArrayList<String> aux = new ArrayList<>();
    aux.add(currentApp);
    Crawler googlePlayStoreCrawler = CrawlerFactory.getCrawler(this.configurationManager, aux, "google");
    googlePlayStoreCrawler.run();
}

Upvotes: 2

Related Questions