简体   繁体   English

如何并行运行Java for循环?

[英]How to run java for loop in parallel?

I am newbie learning selenium and wrote below java code. 我是新手学习Selenium,并在Java代码下面编写。 I am trying to run a for loop that is supposed to load the site 20 times. 我正在尝试运行一个应该将站点加载20次的for循环。 Right now it does loop in sequential order and I want that to be run in parallel. 现在它确实按顺序循环,我希望可以并行运行。 Can you please help. 你能帮忙吗?

public class lenders {

   //ExtentReports logger = ExtentReports.get(lenders.class);
   public static void main(String[] args) throws InterruptedException  {

   for (int i=0; i<20; i++) {
      FirefoxDriver driver= new FirefoxDriver();
      driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);

      try {
         driver.get("https://www.google.com");
      } catch (TimeoutException e) {
           driver.quit();
      }
}

Towards the end I want 20 browsers to be open and loading the site and all of them getting killed. 最后,我希望打开20个浏览器并加载该网站,然后将所有浏览器都杀死。

If you are on Java-8 you can run a parallel for loop using a parallelStream 如果您使用的是Java-8,则可以使用parallelStream运行parallel for循环

 IntStream.range(0,20).parallel().forEach(i->{
         ... do something here
 });

In general and at a high level, trying to run code in parallel within java means that you are trying to run multi-threaded code (more than one thread executing at one time). 一般来说,在较高级别上,尝试在Java中并行运行代码意味着您正在尝试运行多线程代码(一次执行多个线程)。

As individuals have been saying in comments, one must therefore give a warning with my answer. 正如个人在评论中所说的那样,因此必须在我的回答中给出警告。 Multi-threading in itself is a complicated topic and one must enter this territory with caution as there can be many issues/topics regarding "thread safety" and even if this is the way you "should" approach the "business request". 多线程本身是一个复杂的主题,并且必须谨慎进入该领域,因为可能存在许多与“线程安全”有关的问题/主题,即使这是您“应该”处理“业务请求”的方式。

In any case, if you really want to create something multi-threaded then I would direct you to a few technical items to get you STARTED on the topic training (and your own further research): You could create another class that implements the Callable interface. 无论如何,如果您真的想创建多线程的东西,那么我将指导您一些技术性知识,以使您开始进行主题培训(以及您自己的进一步研究):您可以创建另一个实现Callable接口的类。 It will then have to have the "call" method by nature of implementing that interface. 然后,根据实现该接口的性质,它将必须具有“调用”方法。 In this class and in the "call" method you would put the actual logic that you want to happen in parallel. 在此类和“调用”方法中,您将并行放置想要的实际逻辑。 In your case, all of the driver, etc. 在您的情况下,所有驱动程序等

Then in your parent class (the one you put code from above), you can use a FixedThreadPool and an associated ExecutorService that accepts this callable class. 然后,在您的父类(从上面放置代码的类)中,可以使用FixedThreadPool和关联的ExecutorService来接受此可调用类。 It will essentially run the "call" method in a separate thread so that your for loop can continue onwards at the same time that the logic in the "call" method is executed. 它实际上将在单独的线程中运行“调用”方法,以便您的for循环可以在执行“调用”方法中的逻辑的同时继续进行。 It will go the second time around and create another thread, etc. You can manage how many threads are created using your thread pool. 它将第二次运行并创建另一个线程,等等。您可以管理使用线程池创建的线程数。 You can use different kind of thread pools and services, etc. Again, this is a really BIG topic and field. 您可以使用其他类型的线程池和服务等。同样,这是一个非常大的主题和领域。 I am just trying to get your nose in a direction for you to start researching it further. 我只是想让您朝着正确的方向发展。

People might not like my answer because they think you should use completely different technologies other than using Selenium in this manner, etc. I totally understand that point of view and don't disagree with those alternate answers. 人们可能不喜欢我的答案,因为他们认为您应该以不同于这种方式使用Selenium的方式使用完全不同的技术,等等。我完全理解这种观点,并且不同意这些替代答案。 However, your question was "how to get this running at the same time" and I have tried to give you the building block answer. 但是,您的问题是“如何同时运行此程序”,我已尝试为您提供构建基块的答案。 I hope that helps! 希望对您有所帮助! Let me know if you need some links to tutorials or anything, but google "ExecutorService" "Thread Pool" and "Callable" (or combinations of them) with the word java and tutorial should get you a variety of answers on the topic. 让我知道您是否需要一些指向教程或其他内容的链接,但是带有单词java和tutorial的google“ ExecutorService”,“线程池”和“ Callable”(或它们的组合)应该为您提供有关该主题的各种答案。

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM