iOS之dispatch的一些简单运用
1.dispatch_semaphore
当我们在处理一系列线程的时候,当数量达到一定量,在以前我们可能会选择使用NSOperationQueue来处理并发控制,但如何在GCD中快速的控制并发呢?
答案就是dispatch_semaphore,对经常做unix开发的人来讲,我所介绍的内容可能就显得非常入门级了,信号量在他们的多线程开发中再平常不过了。
信号量是一个整形值并且具有一个初始计数值,并且支持两个操作:信号通知和等待。当一个信号量被信号通知,其计数会被增加。
当一个线程在一个信号量上等待时,线程会被阻塞(如果有必要的话),直至计数器大于零,然后线程会减少这个计数。
在GCD中有三个函数是semaphore的操作,分别是:
dispatch_semaphore_create 创建一个semaphore
dispatch_semaphore_signal 发送一个信号
dispatch_semaphore_wait 等待信号
简单的介绍一下这三个函数,第一个函数有一个整形的参数,我们可以理解为信号的总量,
dispatch_semaphore_signal是发送一个信号,自然会让信号总量加1,
dispatch_semaphore_wait等待信号,当信号总量少于0的时候就会一直等待,否则就可以正常的执行,并让信号总量-1。
根据这样的原理,我们便可以快速的创建一个并发控制来同步任务和有限资源访问控制。
dispatch_group_t group = dispatch_group_create();
dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int i = 0; i < 100; i++)
{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(group, queue, ^{
NSLog(@"%i",i);
sleep(2);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
简单的介绍一下这一段代码,创建了一个初使值为10的semaphore,每一次for循环都会创建一个新的线程,线程结束的时候会发送一个信号,
线程创建之前会信号等待,所以当同时创建了10个线程之后,for循环就会阻塞,等待有线程结束之后会增加一个信号才继续执行,
如此就形成了对并发的控制,如上就是一个并发数为10的一个线程队列。
2.dispatch_group_async
dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。这个方法很有用,比如你执行三个下载任务,
当三个任务都下载完成后你才通知界面说完成的了。下面是一段例子代码:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"group1");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"group2");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"group3");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"updateUi");
});
执行结果1:
2018-05-18 21:54:32.161464+0800 dispatch_group[22626:360636] group2
2018-05-18 21:54:32.161464+0800 dispatch_group[22626:360635] group1
2018-05-18 21:54:32.161464+0800 dispatch_group[22626:360638] group3
2018-05-18 21:54:32.161747+0800 dispatch_group[22626:360598] updateUi
执行结果2:
2018-05-18 21:56:37.215815+0800 dispatch_group[22673:362687] group1
2018-05-18 21:56:37.215815+0800 dispatch_group[22673:362686] group3
2018-05-18 21:56:37.215854+0800 dispatch_group[22673:362689] group2
2018-05-18 21:56:37.216242+0800 dispatch_group[22673:362648] updateUi
例子代码2:
NSMutableArray * array = [NSMutableArray array];
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i< 10; i++) {
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
dispatch_group_enter(group);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[array addObject:@(i)];
NSLog(@"execute %d",i);
dispatch_group_leave(group);
});
});
}
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"array %@",array);
NSLog(@"add object success");
});
执行结果:
2018-05-18 21:59:26.836987+0800 dispatch_group[22724:364531] execute 0
2018-05-18 21:59:26.837411+0800 dispatch_group[22724:364531] execute 1
2018-05-18 21:59:26.837593+0800 dispatch_group[22724:364531] execute 2
2018-05-18 21:59:26.837700+0800 dispatch_group[22724:364531] execute 3
2018-05-18 21:59:26.837817+0800 dispatch_group[22724:364531] execute 4
2018-05-18 21:59:26.838187+0800 dispatch_group[22724:364531] execute 6
2018-05-18 21:59:26.838336+0800 dispatch_group[22724:364531] execute 8
2018-05-18 21:59:26.838803+0800 dispatch_group[22724:364531] execute 9
2018-05-18 21:59:26.839035+0800 dispatch_group[22724:364531] execute 5
2018-05-18 21:59:26.839111+0800 dispatch_group[22724:364531] execute 7
2018-05-18 21:59:26.839476+0800 dispatch_group[22724:364564] array (
0,
1,
2,
3,
4,
6,
8,
9,
5,
7
)
2018-05-18 21:59:26.839584+0800 dispatch_group[22724:364564] add object success
3.dispatch_barrier_async
dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行
//例子代码如下:
dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"dispatch_async1");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"dispatch_async2");
});
dispatch_barrier_async(queue, ^{
NSLog(@"dispatch_barrier_async");
[NSThread sleepForTimeInterval:4];
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"dispatch_async3");
});
打印结果:
2018-05-18 21:48:26.270474+0800 dispatch_group[22499:355648] dispatch_async1
2018-05-18 21:48:28.274938+0800 dispatch_group[22499:355647] dispatch_async2
2018-05-18 21:48:28.275130+0800 dispatch_group[22499:355647] dispatch_barrier_async
2018-05-18 21:48:33.283809+0800 dispatch_group[22499:355647] dispatch_async3
请注意执行的时间,可以看到执行的顺序如上所述。
4.dispatch_apply
执行某个代码片段N次
dispatch_apply(5, dispatch_get_global_queue(0, 0), ^(size_t index) {
// 执行5次
NSLog(@"下班下班 %ld",index);
});
图4.png
未完