1

I am trying to create an asynchronous call first time. I came across CompletableFuture [https://stackoverflow.com/questions/59183298/how-to-get-result-from-completablefuturelistcustomobject-in-java-8]. Hence, I tried as follows:

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class test {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Hashtable<Integer, Integer> hash_table =
                new Hashtable<Integer, Integer>();
        hash_table.put(0, 10);
        hash_table.put(1, 20);
        hash_table.put(2, 30);
        int sum = 0;
        List<CompletableFuture<Integer>> val = new ArrayList<>();
        for (int i = 0; i < hash_table.size(); i++) {
            final int finali = i;
            val.add(CompletableFuture.supplyAsync(() -> computeArea(hash_table.get(finali))));
        }
        for (int i=0;i<val.size();i++){
            sum+= val.get(i).get();
        }
        System.out.println(sum);
    }
    private static int computeArea(int a) {
        return a * a;
    }
}

Further questions:

  1. Can we use runAsync() here? If not, when should we use it?
  2. Why do people use allOf()? Should I also use it to check if any of the tasks have exceptions? I sometimes get unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
  3. What is a more efficient way to calculate the sum of all values in val? Can I use thenApply()?
harry123
  • 760
  • 1
  • 7
  • 22
  • 1
    Last loop will not even compile. – Michał Krzywański Sep 08 '20 at 18:36
  • 1
    @amyj please, provide us a compilable code. If any libraries are used - provide thiers name. – Jakub Biały Sep 08 '20 at 18:40
  • @michalk You are correct. this is my first time using the CompletableFuture so I tried to use the referenced example. Sorry! It will be great if now I can get some help. – harry123 Sep 08 '20 at 21:01
  • @JakubBiały Sorry, this is the first time to use the library as well. Forgot to add it. The entire code is too long so I have created the placeholder function to understand on how can I use this interface. – harry123 Sep 08 '20 at 21:03
  • @michalk I read that `allof()` is used to process after all tasks are completed. But I am not sure how can I combine `allOf` and calculate the sum. – harry123 Sep 09 '20 at 04:49
  • `allOf` returns a `CompletableFuture` which completes when all of the tasks are completed. So when you call `get` on the returned `CompletableFuture` it will block calling thread unitl all composed tasks are finished. – Michał Krzywański Sep 09 '20 at 17:36
  • @michalk any efficient way to sum the values? – harry123 Sep 09 '20 at 17:40

1 Answers1

0

Can we use runAsync() here? If not, when should we use it?

Yes, you would use run async. The purpose of the method is to run each task in parallel rather than one after another sequentially.

Why do people use allOf()? Should I also use it to check if any of the tasks have exceptions?

The question is subjective but you can use it to check if exceptions have finished erroneously. You can also use it to ensure that all tasks have completed, or if some have not. There are a lot of applications for CompletableFuture#allOf.

Jason
  • 5,154
  • 2
  • 12
  • 22
  • Sorry but I changed the question as requested by other users. Is there any way in which you can help with the last question with an example. – harry123 Sep 09 '20 at 04:36