简体   繁体   English

按启动顺序获得线程的结果

[英]Obtaining the results of the threads in the sequence they were started

I am accessing data from an api with the below mentioned endpoints:- 我正在通过具有以下提到的端点的api访问数据:-

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. 我应该创建一个哈希表的数组列表,其中包含每个产品的productid,productprice和productimageurl。从主线程中生成线程A到第一个端点,并在解析结果时从线程A中生成线程。

However the sequence in which those threads respond back to threadA is not the same in which they were started. 但是,这些线程响应线程A的顺序与启动它们的顺序不同。 So i end up having the imageurl of product 2 added up to product with id 1. 所以我最终将产品2的imageurl添加到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 Executor to spawn your threads, you can save all the returned Future s and then call Future.get() on them in the same order as they were started. 如果使用Executor生成线程,则可以保存所有返回的Future ,然后按照启动时的顺序对其调用Future.get()

Read more here: ExecutorService and Future . 在此处了解更多信息: ExecutorServiceFuture

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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