0

I have a rest call api where max count of result return by the api is 1000.start page=1

{ "status": "OK", "payload": { "EMPList":[], count:5665 }

So to get other result I have to change the start page=2 and again hit the service.again will get 1000 results only.

but after first call i want to make it as a parallel call and I want to collect the result and combine it and send it back to calling service in java. Please suggest i am new to java.i tried using callable but it's not working

Nitin
  • 71
  • 1
  • 10
  • CompletableFuture getToDoAsync(String id){ CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() { @Override public ToDo get() { final ToDo toDo = restApiClient.getToDo(id); return toDo; } }); return future; }. i was trying like this – Nitin Jun 18 '20 at 08:01

1 Answers1

0

It seems to me that ideally you should be able to configure your max count to one appropriate for your use case. I'm assuming you aren't able to do that. Here is a simple, lock-less, multi threading scheme that acts as a simple reduction operation for your two network calls:

// online runnable: https://ideone.com/47KsoS
int resultSize = 5;
int[] result = new int[resultSize*2];

Thread pg1 = new Thread(){
    public void run(){
        System.out.println("Thread 1 Running...");
        // write numbers 1-5 to indexes 0-4
        for(int i = 0 ; i < resultSize; i ++) {
            result[i] = i + 1;
        }
        System.out.println("Thread 1 Exiting...");
    }
};

Thread pg2 = new Thread(){
    public void run(){
        System.out.println("Thread 2 Running");
        // write numbers 5-10 to indexes 5-9
        for(int i = 0 ; i < resultSize; i ++) {
            result[i + resultSize] = i + 1 + resultSize;
        }
        System.out.println("Thread 2 Exiting...");
    }
};

pg1.start();
pg2.start();

// ensure that pg1 execution finishes
pg1.join();
// ensure that pg2 execution finishes
pg2.join();

// print result of reduction operation
System.out.println(Arrays.toString(result));

There is a very important caveat with this implementation however. You will notice that both of the threads DO NOT overlap in their memory writes. This is very important as if you were to simply change our int[] result to ArrayList<Integer> this could lead to catastrophic failure in our reduction operation between the two threads called a Race Condition (I believe the standard ArrayList implementation in Java is not thread safe). Since we can guarantee how large our result will be I would highly suggest sticking to my usage of an array for this multi-threaded implementation as ArrayLists hide a lot of implementation logic from you that you likely won't understand until you take a basic data-structures course.

Bennett Yeo
  • 819
  • 2
  • 14
  • 28