I am accessing data from an api with the below mentioned endpoints:-
1.getProductList
2.getImageurlofProduct/productid
I am supposed to create an arraylist of hashmaps which contain productid,productprice and productimageurl of each product.From the main thread I spawn a thread-A to the first endpoint and as I parse through the result I spawn threads from thread A.
However the sequence in which those threads respond back to threadA is not the same in which they were started. So i end up having the imageurl of product 2 added up to product with id 1.
How do i handle this. An algorithm should do fine for me.
Please let me know if you want me to clarify something more
If you use an
Executorto spawn your threads, you can save all the returnedFutures and then callFuture.get()on them in the same order as they were started.Read more here:
ExecutorServiceandFuture.