[英]Run 1 thread at time with ExecutorService
I'm developing a review extractor for the Play Store, using Selenium. 我正在使用Selenium开发Play商店的评论提取器。 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
. 在这里,
Crawler
实现了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). 使用Selenium的线程多于线程,每个线程都打开一个单独的Firefox实例,Selenium失败,因为该网页不可见(该页面被另一个线程打开的新窗口隐藏了)。 So, I'm trying to execute all the process with only 1 thread at time.
因此,我试图一次仅用1个线程执行所有过程。
But also if I instantiate an ExecutorService
using newFixedThreadPool
with 1 as parameter, a new threads always starts when the previous is running. 但是,如果我使用
newFixedThreadPool
以1作为参数实例化ExecutorService
,则新线程总是在前一个线程运行时启动。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.