简体   繁体   中英

ExecutorService submit() - How to make it non blocking?

I'm trying to make the following code non-blocking. I'm using ExecutorService.

In my constructor:

this.executor = Executors.newFixedThreadPool(5);
executor.awaitTermination(10, TimeUnit.SECONDS);
          

then I add all the items I want to run in parallel to a list:

   Future<Map.Entry<Location, SomeData>> result = this.executor.submit(new Sender(entry));
   resultList.add(result);

Then I loop on that list and use the get() function of Future in order to execute each task - which appears to be blocking:

 for (int i = 0; i < resultList.size(); i++) {

        Future<Map.Entry<Location, SomeData>> result = resultList.get(i);

        try {
            logger.info("[Start] Task" + sendQueue.get(i).getKey() + "-" + i);
            entry = result.get();
        } catch (InterruptedException e) {
            logger.error("Interrupted error", e);
        } catch (ExecutionException e) {
            logger.error("Thread Execution error", e);
        } catch (Exception e) {
            logger.error("Send Error", e);
        }

        if (entry == null) {
            logger.error("Telemetry Send Error");
            return;
        }

        logger.info("[Success] Task" + entry.getKey() + "-" + i);
    }

Sender call():

@Override
    public Map.Entry<Location, Data> call() throws InterruptedException {
        Thread.sleep(5000);
        return this.entry;
    }

Then my program runs all this code somewhere like this:

public void myMainProgram() {
    System.out.println("Started");
    this.startAllExecutorTasks();
    System.out.println("Ended - More code that should run without waiting for the tasks");
}

I expect the startAllExecutorTasks method (which runs all the code above) to be non-blocking, which means the "started" and "Ended" prints should be printed one after the other. I want my program to continue working although my executor running his own tasks.

Any idea how I can make my thread executor code non blocking?

I expect the startAllExecutorTasks method (which runs all the code above) to be non-blocking, which means the "started" and "Ended" prints should be printed one after the other.

The reason why it's blocking is because you wait for the results from the Future s.

If you want to make the method call not block, while also still have it wait for the results, make the method call in a background thread:

void startAllExecutorTasksInBackground() {
    Executors.newSingleThreadExecutor()
             .submit(this::startAllExecutorTasks);
}

public void myMainProgram() {
    System.out.println("Started");
    this.startAllExecutorTasksInBackground();
    System.out.println("Ended - More code that should run without waiting for the tasks");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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