GCD进阶

作者: 曼谷第一开膛手 | 来源:发表于2018-08-21 22:23 被阅读11次

GCD 控制线程数量

GCD 不像NSOperation 那样有直接提供线程数量控制方法,但是通过GCD的semaphore 功能一样可以达到控制线程数量的效果。

  1. dispatch_semaphore_create(long value);利用给定的输出时创建一个新的可计数的信号量
  2. dispatch_semaphore_wait(dispatch_semaphore_t dsema,dispatch_time_t timeout);如果信号量大于0,信号量减1,执行程序。否则等待信号量
  3. 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>

相关文章

  • iOS多线程之GCD - 基础

    GCD系列 GCD基础 GCD进阶函数 调度组dispatch_group 信号量dispatch_semapho...

  • Swift多线程:GCD进阶,单例、信号量、任务组

    Swift多线程:GCD进阶,单例、信号量、任务组 Swift多线程:GCD进阶,单例、信号量、任务组

  • OC 多线程之 GCD知识从基础到进阶 (1)

    OC 多线程之 GCD知识从基础到进阶 (1) 概念认知 基本概念: GCD 全称 (Grand Center D...

  • GCD进阶

    GCD 控制线程数量 GCD 不像NSOperation 那样有直接提供线程数量控制方法,但是通过GCD的sema...

  • GCD进阶指南

    1. 并行和并发 简单来说,若说两个任务A和B并发执行,则表示任务A和任务B在同一时间段里被执行(更多的可能是二者...

  • 《iOS进阶》-GCD使用

    GCD的使用 block的定义 block 有点像函数指针,只不过用"^"代替了""* 申明变量:(void)(^...

  • iOS+面试+思维 进阶之路

    iOS+思维 进阶篇 原理篇 第一节:《Objective-C 高级编程》学习笔记Objective-C之GCD多...

  • iOS开发进阶-GCD

    什么是GCD GCD是大家现在常用的一种多线程编程技术之一,GCD用非常简洁的记述方法,实现了极为复杂繁琐的多线程...

  • Swift GCD的进阶用法

    1、信号量(Semaphore) 定义:信号量就是一个资源计数器,就是一种可用来控制访问资源数量的标识。 通过添加...

  • iOS进阶-多线程-GCD

    GCD 概念 GCD队列 使用总结 实用api 实用遇到的坑 一、 概念 GCD(grand central di...

网友评论

    本文标题:GCD进阶

    本文链接:https://www.haomeiwen.com/subject/uwktiftx.html