我想到的
for iOS
1.用dispatch_group实现
2.用RunLoop实现
还有没有其他的比较好的实现方式,求关于并发编程的文章.
伊谢尔伦2017-04-17 16:17:12
Then network requests are all asynchronous, are they handled in the same way?
迷茫2017-04-17 16:17:12
Build a block or closure. Send a network request and call itself in completionHandler to send the next request.
PHP中文网2017-04-17 16:17:12
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[request1 completed:^(BOOL sucess, id response){
dispatch_grpup_leave(group);
}];
dispatch_group_enter(group);
[request2 completed:^(BOOL sucess, id response){
dispatch_grpup_leave(group);
}];
dispatch_group_enter(group);
[request3 completed:^(BOOL sucess, id response){
dispatch_grpup_leave(group);
}];
dispatch_group_enter(group);
[request4 completed:^(BOOL sucess, id response){
dispatch_grpup_leave(group);
}];
.
.
.
.
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[do something];
});
高洛峰2017-04-17 16:17:12
Use AFNetworking
的可以用AFURLConnectionOperation batchOfRequestOperations: progressBlock:completionBlock:
ringa_lee2017-04-17 16:17:12
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
PHP中文网2017-04-17 16:17:12
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.
天蓬老师2017-04-17 16:17:12
Java’s own concurrency framework supports solving the problem you mentioned, Future