// 方法一 dispatch_group_async & dispatch_group_notify 队列中任务执行完之后通知dispatch_group_notify的block内部方法执行
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"group one start");
dispatch_group_async(group, queue, ^{
sleep(3.7);
NSLog(@"任务一");
});
dispatch_group_async(group, queue, ^{
sleep(3);
NSLog(@"任务二");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"任务san");
});
// 方法二
dispatch_group_t group2 = dispatch_group_create();
dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group2);
dispatch_async(queue2, ^{
sleep(3.0);
NSLog(@"任务一");
dispatch_group_leave(group2);
});
dispatch_group_enter(group2);
dispatch_async(queue2, ^{
sleep(3.0);
NSLog(@"任务二");
dispatch_group_leave(group2);
});
dispatch_group_enter(group2);
dispatch_async(queue2, ^{
sleep(3.0);
NSLog(@"任务三");
dispatch_group_leave(group2);
});
dispatch_group_notify(group2, queue2, ^{
NSLog(@"最后的任务");
});
// 方法三栅栏函数
// 通过dispatch_barrier_async添加的block会等到之前添加所有的block执行完毕再执行
// 在dispatch_barrier_async之后添加的block会等到dispatch_barrier_async添加的block执行完毕再执行
dispatch_queue_t queue = dispatch_queue_create("xukun", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
sleep(3.0);
NSLog(@"任务一");
});
dispatch_async(queue, ^{
sleep(5.0);
NSLog(@"任务二");
});
dispatch_async(queue, ^{
NSLog(@"任务三");
});
dispatch_barrier_async(queue, ^{
NSLog(@"任务si");
});
dispatch_async(queue, ^{
NSLog(@"任务wu");
});
dispatch_async(queue, ^{
NSLog(@"任务liu");
});
// 当我们多个线程要访问同一个资源的时候,往往会设置一个信号量,当信号量大于0的时候,新的线程可以去操作这个资源,操作时信号量-1,操作完后信号量+1,当信号量等于0的时候,必须等待,所以通过控制信号量,我们可以控制能够同时进行的并发数。
//
// crate的value表示,最多几个资源可访问
// 由于设定的信号值为2,先执行两个线程,等执行完一个,才会继续执行下一个,保证同一时间执行的线程数不超过2
// dispatch_semaphore_create创建信号量,初始值不能小于0;
//
// dispatch_semaphore_wait等待降低信号量,也就是信号量-1;
//
// dispatch_semaphore_signal提高信号量,也就是信号量+1;
//
// dispatch_semaphore_wait和dispatch_semaphore_signal通常配对使用。
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//任务1
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 1");
sleep(1);
NSLog(@"complete task 1");
dispatch_semaphore_signal(semaphore);
});
//任务2
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 2");
sleep(1);
NSLog(@"complete task 2");
dispatch_semaphore_signal(semaphore);
});
//任务3
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 3");
sleep(1);
NSLog(@"complete task 3");
dispatch_semaphore_signal(semaphore);
});
1、提起ios中多个异步函数后的同步问题,自然会想到 dispatch group 这个概念,那么它能够解决异步网络请求的问题吗?
对于dispatch多个异步操作后的同步方法,以前只看过dispatch_group_async,看看这个方法的说明:
可以看出,dispatch_group_async,是用于同步工作的,但是,它的判断标准是放入的block是否执行完毕,如果我们放入block中包含异步的网络请求,这个方法无法在网络数据返回后再进行同步。
看一段使用dispatch_group_async处理网络问题的代码:
dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
[NSThread detachNewThreadWithBlock:^{
sleep(2);
NSLog(@"================1" );
}];
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
[NSThread detachNewThreadWithBlock:^{
sleep(2);
NSLog(@"================2" );
}];
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"end");
});
从打印结果可以看出,并不满足需求。看一下下面正确的方法
2、
dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_enter(dispatchGroup);
[NSThread detachNewThreadWithBlock:^{
sleep(2);
NSLog(@"================1" );
dispatch_group_leave(dispatchGroup);
}];
dispatch_group_enter(dispatchGroup);
[NSThread detachNewThreadWithBlock:^{
sleep(2);
NSLog(@"================2" );
dispatch_group_leave(dispatchGroup);
}];
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"end");
});
3、、信号量机制
dispatch_semaphore_t semaphore = dispatch_semaphore_create(3);
dispatch_group_t dispatchGroup = dispatch_group_create();
for (int i = 0; i < 10; i++) {
[NSThread detachNewThreadWithBlock:^{
sleep(2);
NSLog(@"================%d",i);
dispatch_group_leave(dispatchGroup);
dispatch_semaphore_signal(semaphore);
}];
dispatch_group_enter(dispatchGroup);//为了所有下载完成后能调用函数,引入 dispatch group。如果信号量是1的话,可以不使用这个机制,也能达到效果。
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); //为了最大并发数,加入信号量
}
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"end");
});
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self loadCommentCount:^{
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self loadDynamicId:^{
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self requestCheckIsFollow:^{
dispatch_group_leave(group);
}];
@weakify(self);
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
@strongify(self);
[self.dataLoadedSignal sendNext:@(YES)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self needEndDetailPageLoading];
});
});
};
4\NSOperationQueue 并发队列为1
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 2.设置最大并发操作数
queue.maxConcurrentOperationCount = 1; // 串行队列
// queue.maxConcurrentOperationCount = 2; // 并发队列
// queue.maxConcurrentOperationCount = 8; // 并发队列
// 3.添加操作
[queue addOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
}
}];
[queue addOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@", [NSThread currentThread]); // 打印当前线程
}
}];
[queue addOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"3---%@", [NSThread currentThread]); // 打印当前线程
}
}];
[queue addOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"4---%@", [NSThread currentThread]); // 打印当前线程
}
}];