GCD 控制线程数量
GCD 不像NSOperation 那样有直接提供线程数量控制方法,但是通过GCD的semaphore 功能一样可以达到控制线程数量的效果。
- dispatch_semaphore_create(long value);利用给定的输出时创建一个新的可计数的信号量
- dispatch_semaphore_wait(dispatch_semaphore_t dsema,dispatch_time_t timeout);如果信号量大于0,信号量减1,执行程序。否则等待信号量
- dispatch_semaphore_signal(dispatch_semaphore_t dsema);增加信号量
信号量其实就是用来保证访问资源的线程数,当信号量大于等于1时,资源可以访问,否则无法访问资源,直到其它线程释放资源。
这里主要有三个函数:
long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout); //信号量-1
long dispatch_semaphore_signal(dispatch_semaphore_t dsema); //信号量+1
// 控制线程数量
- (void)runMaxThreadCountWithGCD
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentRunMaxThreadCountWithGCD", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t serialQueue = dispatch_queue_create("serialRunMaxThreadCountWithGCD", DISPATCH_QUEUE_SERIAL);
// 创建一个semaphore,并设置最大信号量,最大信号量表示最大线程数量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
// 使用循环往串行队列 serialQueue 增加 10 个任务
for (int i = 0; i < 10 ; i++) {
dispatch_async(serialQueue, ^{
// 只有当信号量大于 0 的时候,线程将信号量减 1,程序向下执行
// 否则线程会阻塞并且一直等待,直到信号量大于 0
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(concurrentQueue, ^{
NSLog(@"%@ 执行任务一次 i = %d",[NSThread currentThread],i);
// 当线程任务执行完成之后,发送一个信号,增加信号量。
dispatch_semaphore_signal(semaphore);
});
});
}
NSLog(@"%@ 执行任务结束",[NSThread currentThread]);
}
执行结果如下,只有 number 3 和 number 4 这 2 个线程在执行
<nsthread: 0x60c00007c600>{number = 1, name = main} 执行任务结束
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 执行任务一次 i = 0
<nsthread: 0x608000263a00>{number = 4, name = (null)} 执行任务一次 i = 1
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 执行任务一次 i = 3
<nsthread: 0x608000263a00>{number = 4, name = (null)} 执行任务一次 i = 2
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 执行任务一次 i = 4
<nsthread: 0x608000263a00>{number = 4, name = (null)} 执行任务一次 i = 5
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 执行任务一次 i = 6
<nsthread: 0x608000263a00>{number = 4, name = (null)} 执行任务一次 i = 7
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 执行任务一次 i = 8
<nsthread: 0x608000263a00>{number = 4, name = (null)} 执行任务一次 i = 9</nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x60c00007c600>
GCD 任务分组
GCD 的 dispatch_group_t 功能可以将多个任务分组,等待分组里面的所有任务执行完成之后,GCD 的 dispatch_group_notify 方法可以通知。通常会配合一些常见的场景来考察,比如同时上传 10 张图片,全部上传完成后通知用户。
// 任务分组
- (void)runGroupWithGCD
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("runGroupWithGCD", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 10 ; i++) {
dispatch_group_async(group, concurrentQueue, ^{
NSLog(@"%@ 执行任务一次",[NSThread currentThread]);
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%@ 执行任务结束",[NSThread currentThread]);
});
}
将所有的任务都加入 group ,等待所有的任务执行完成后,dispatch_group_notify 会被调用。
<nsthread: 0x608000265180>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x604000079a40>{number = 6, name = (null)} 执行任务一次
<nsthread: 0x60c000268780>{number = 5, name = (null)} 执行任务一次
<nsthread: 0x60c000267dc0>{number = 3, name = (null)} 执行任务一次
<nsthread: 0x608000265080>{number = 9, name = (null)} 执行任务一次
<nsthread: 0x600000265480>{number = 7, name = (null)} 执行任务一次
<nsthread: 0x60c00007f9c0>{number = 8, name = (null)} 执行任务一次
<nsthread: 0x608000264f40>{number = 10, name = (null)} 执行任务一次
<nsthread: 0x604000079a40>{number = 6, name = (null)} 执行任务一次
<nsthread: 0x608000265180>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x60000006d180>{number = 1, name = main} 执行任务结束</nsthread: 0x60000006d180></nsthread: 0x608000265180></nsthread: 0x604000079a40></nsthread: 0x608000264f40></nsthread: 0x60c00007f9c0></nsthread: 0x600000265480></nsthread: 0x608000265080></nsthread: 0x60c000267dc0></nsthread: 0x60c000268780></nsthread: 0x604000079a40></nsthread: 0x608000265180>
GCD 任务分组和线程数量控制
利用 GCD 的 dispatch_group_t 和 semaphore 功能,我们可以做到控制线程数量,并且在所有任务执行完成之后得到通知。
// 任务分组 + 线程数量控制
- (void)runMaxCountInGroupWithGCD
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("runGroupWithGCD", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
for (int i = 0; i < 10 ; i++) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(group, concurrentQueue, ^{
NSLog(@"%@ 执行任务一次",[NSThread currentThread]);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%@ 执行任务结束",[NSThread currentThread]);
});
}
执行之后,我们可以看到既控制了线程数量,也在执行任务完成之后得到了通知。
<nsthread: 0x604000269b40>{number = 3, name = (null)} 执行任务一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 执行任务一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 执行任务一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 执行任务一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 执行任务一次
<nsthread: 0x60400007aa40>{number = 1, name = main} 执行任务结束</nsthread: 0x60400007aa40></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x608000264780></nsthread: 0x608000264780></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x604000269b40>
网友评论