It can be easily achieved using RxJava in Android. Of course, you can also try the thread synchronization auxiliary class CountDownLatch implementation. For the use of CountDownLatch, you can refer to the following blog: http://www.liuling123.com/2013/08/countdownlatch-demo.html
I was shocked when I saw the answers from the masters. Faced with such problems, I often implement them manually. I usually set a resource variable, initialize the resource to ten, run a thread to monitor the number of resources, and then start concurrent tasks. Each time it is completed, Decrease a resource by one. When the resource reaches zero, stop the listening thread and complete subsequent operations.
This is a stupid way. I will use this when I don't know much about a language. After all, most languages can be implemented this way.
Then network requests are all asynchronous, are they handled in the same way?
Build a block or closure. Send a network request and call itself in completionHandler to send the next request.
Android definitely RxJava. iOS RxSwift?
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[request1 completed:^(BOOL sucess, id response){
}];
dispatch_group_enter(group);
[request2 completed:^(BOOL sucess, id response){
}];
dispatch_group_enter(group);
[request3 completed:^(BOOL sucess, id response){
}];
dispatch_group_enter(group);
[request4 completed:^(BOOL sucess, id response){
}];
.
.
.
.
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
});
I feel it is better to use GCD, because it can always be in c/c++ code
Use
AFNetworking
的可以用AFURLConnectionOperation batchOfRequestOperations: progressBlock:completionBlock:
Use GCD’s group
It can be easily achieved using RxJava in Android.
Of course, you can also try the thread synchronization auxiliary class CountDownLatch implementation. For the use of CountDownLatch, you can refer to the following blog:
http://www.liuling123.com/2013/08/countdownlatch-demo.html
I was shocked when I saw the answers from the masters. Faced with such problems, I often implement them manually. I usually set a resource variable, initialize the resource to ten, run a thread to monitor the number of resources, and then start concurrent tasks. Each time it is completed, Decrease a resource by one. When the resource reaches zero, stop the listening thread and complete subsequent operations.
This is a stupid way. I will use this when I don't know much about a language. After all, most languages can be implemented this way.
Java’s own concurrency framework supports solving the problem you mentioned, Future