PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to run two ExecutorService invokeAll() in parallel?

 October 24, 2022     concurrency, executorservice, java     No comments   

Issue

TLDR: I want to submit two lists of callables at the same time, but different timeout


Is there a way or the best alternative to running two invokeAll() commands at the same time with different timeouts?

Ex of blocking:

ExecutorService executorService1 = Executors.newFixedThreadPool(2);
ExecutorService executorService2 = Executors.newFixedThreadPool(2);

List<Callable<String>> callableTasks1;
List<Callable<String>> callableTasks2;

List<Future<String>> completed;

completed = executorService1.invokeAll(callableTasks1, 5, TimeUnit.Seconds);
completed.addAll(executorService1.invokeAll(callableTasks2, 2, TimeUnit.Seconds));
for(Future<String> s: completed) {
    if(s.isCancelled()) {
        System.out.println("It's cancelled");
    } else {
       try {
         System.out.println("Got it: " + s.get());
       } 
       catch(...) {
        ...
       }
    }
}

Submitting each task in a for loop: executorService1.submit(task) and calling task.get(5, TimeUnit.Seconds) seems to be running in sequence.


Solution

The solution I ended up with was waiting for the results from two threads. Also instead of creating two threads for each call I ended up using another executorService to reuse the threads

Shortened version(doesn't run, modify to run):

ExecutorService executorService1 = Executors.newFixedThreadPool(5);
ExecutorService executorService2 = Executors.newFixedThreadPool(2);

List<Callable<String>> callableTasks1 = new ArrayList(3 callables);
List<Callable<String>> callableTasks2 = new ArrayList(2 callables);

List<Callable<Object>> callableObjectFutures = executorService2.invokeAll(
() -> {executorService1.invokeAll(invokeAll(callableTasks1, 5, TimeUnit.Seconds);},
() -> {executorService1.invokeAll(invokeAll(callableTasks2, 2, TimeUnit.Seconds);}
);

List<Future<String>> completed = callableObjectFutures.get(0).get();
completed.addAll(callableObjectFutures.get(1).get());


Answered By - Gunther
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing