iOS GCD学习记录

移动开发 来源:旅途开发者 1140℃ 0评论

1,对于创建全局使用的串行或者并行队列,都应该用strong修饰,例如

  • @property (nonatomic,strong) dispatch_queue_t dispatch_serial

这是因为在ios6.0之前,oc是没办法自动管理gcd队列的,所以在iOS6.0之前要使用assign。但是iOS6.0之后,苹果在oc里面加入了自动管理gcd的功能。

2,有关内容的学习

这里首先要记住
串行队列是:DISPATCH_QUEUE_SERIAL
并行队列是:DISPATCH_QUEUE_CONCURRENT
同步执行:dispatch_sync(<这里写是串行还是并行>, {<代码要完成的事>})
异步执行:dispatch_async(<这里写是串行还是并行>, {<代码要完成的事>})

注:只要是异步执行都会创建新的线程,同步执行不会创建新的线程。串行队列肯定是上一个任务执行完才会执行下一个任务,并行队列理论上可以说是同时执行,任务完成的顺序和cpu的分配有关

首选声明两个全局使用的队列,一个串行队列,一个并行队列

@property (nonatomic,strong) dispatch_queue_t dispatch_serial;/**<串行队列*/
@property (nonatomic,strong) dispatch_queue_t dispatch_concurrent;/**<并行队列*/

在viewDidLoad里面创建这两个队列

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建两个队列
    _dispatch_serial = dispatch_queue_create("dispatch_serial", DISPATCH_QUEUE_SERIAL);
    _dispatch_concurrent = dispatch_queue_create("dispatch_concurrent", DISPATCH_QUEUE_CONCURRENT);
    
    [self serialAndSynchronize];
    [self serialAndASynchronize];
    [self concurrentAndSynchronize];
    [self concurrentAndASynchronize];
    
}

下面就是对不同情况下简单的使用

串行同步队列

//串行同步队列
-(void)serialAndSynchronize{
    dispatch_sync(_dispatch_serial, ^{
        NSLog(@"1---------%@",[NSThread currentThread]);
    });
    dispatch_sync(_dispatch_serial, ^{
        NSLog(@"2---------- %@",[NSThread currentThread]);
    });
    dispatch_sync(_dispatch_serial, ^{
        NSLog(@"3---------- %@",[NSThread currentThread]);
    });
    NSLog(@"4------------ %@",[NSThread currentThread]);
}

打印的数据

2017-08-31 14:23:52.340 SYGCDStudy[8903:207494] 1---------<NSThread: 0x60000006c700>{number = 1, name = main}
2017-08-31 14:23:52.340 SYGCDStudy[8903:207494] 2---------- <NSThread: 0x60000006c700>{number = 1, name = main}
2017-08-31 14:23:52.340 SYGCDStudy[8903:207494] 3---------- <NSThread: 0x60000006c700>{number = 1, name = main}
2017-08-31 14:23:52.341 SYGCDStudy[8903:207494] 4------------ <NSThread: 0x60000006c700>{number = 1, name = main}

这里可以看到。串行同步队列实际上都是在主线程上面执行的,而且没有创建新的线程,并行都是按照顺序执行的,必须等前一个执行结束,才会执行下一个

串行异步队列

//串行异步队列
-(void)serialAndASynchronize{
    dispatch_async(_dispatch_serial, ^{
        NSLog(@"1------------ %@",[NSThread currentThread]);
    });
    dispatch_async(_dispatch_serial, ^{
        NSLog(@"2------------ %@",[NSThread currentThread]);
    });
    dispatch_async(_dispatch_serial, ^{
        NSLog(@"3------------ %@",[NSThread currentThread]);
    });
    NSLog(@"4------------ %@",[NSThread currentThread]);
}

打印的数据

2017-08-31 14:25:02.175 SYGCDStudy[8925:209538] 1------------ <NSThread: 0x600000267000>{number = 3, name = (null)}
2017-08-31 14:25:02.175 SYGCDStudy[8925:209484] 4------------ <NSThread: 0x60800007ca80>{number = 1, name = main}
2017-08-31 14:25:02.175 SYGCDStudy[8925:209538] 2------------ <NSThread: 0x600000267000>{number = 3, name = (null)}
2017-08-31 14:25:02.175 SYGCDStudy[8925:209538] 3------------ <NSThread: 0x600000267000>{number = 3, name = (null)}

先打印了4,然后顺序在子线程中打印1,2,3。说明异步执行具有开辟新线程的能力,并且串行队列必须等到前一个任务执行完才能开始执行下一个任务,同时,异步执行会使内部函数率先返回,不会与正在执行的外部函数发生死锁。

并行同步队列

//并行同步队列
-(void)concurrentAndSynchronize{
    dispatch_sync(_dispatch_concurrent, ^{
        NSLog(@"1------------ %@",[NSThread currentThread]);
    });
    dispatch_sync(_dispatch_concurrent, ^{
       NSLog(@"2------------ %@",[NSThread currentThread]);
    });
    dispatch_sync(_dispatch_concurrent, ^{
        NSLog(@"3------------ %@",[NSThread currentThread]);
    });
    NSLog(@"4------------ %@",[NSThread currentThread]);
}

打印的数据

2017-08-31 14:30:10.572 SYGCDStudy[8953:213767] 1------------ <NSThread: 0x60800007c180>{number = 1, name = main}
2017-08-31 14:30:10.572 SYGCDStudy[8953:213767] 2------------ <NSThread: 0x60800007c180>{number = 1, name = main}
2017-08-31 14:30:10.573 SYGCDStudy[8953:213767] 3------------ <NSThread: 0x60800007c180>{number = 1, name = main}
2017-08-31 14:30:10.573 SYGCDStudy[8953:213767] 4------------ <NSThread: 0x60800007c180>{number = 1, name = main}

未开启新的线程执行任务,并且Block函数执行完成后dispatch函数才会返回,才能继续向下执行,所以我们看到的结果是顺序打印的。

并行异步队列

//并行异步队列
-(void)concurrentAndASynchronize{
    dispatch_async(_dispatch_concurrent, ^{
       NSLog(@"1------------ %@",[NSThread currentThread]);
    });
    dispatch_async(_dispatch_concurrent, ^{
       NSLog(@"2------------ %@",[NSThread currentThread]);
    });
    dispatch_async(_dispatch_concurrent, ^{
        NSLog(@"3------------ %@",[NSThread currentThread]);
    });
    NSLog(@"4------------ %@",[NSThread currentThread]);
}

打印的数据

2017-08-31 14:34:08.966 SYGCDStudy[9008:220555] 3------------ <NSThread: 0x608000079380>{number = 5, name = (null)}
2017-08-31 14:34:08.966 SYGCDStudy[9008:220478] 4------------ <NSThread: 0x60000006da80>{number = 1, name = main}
2017-08-31 14:34:08.966 SYGCDStudy[9008:220557] 2------------ <NSThread: 0x600000077340>{number = 4, name = (null)}
2017-08-31 14:34:08.966 SYGCDStudy[9008:220554] 1------------ <NSThread: 0x608000079040>{number = 3, name = (null)}

开辟了多个线程,触发任务的时机是顺序的,但是我们看到完成任务的时间却是随机的,这取决于CPU对于不同线程的调度分配,但是,线程不是无条件无限开辟的,当任务量足够大时,线程是会重复利用的。

3,其他一些常用的gcd方法

dispatch_once ,只执行一次的方法

//只执行一次
- (IBAction)carryOutOne:(id)sender {
    static dispatch_once_t oneDispatch;
    dispatch_once(&oneDispatch, ^{
        NSLog(@"这个方法只会执行一次");
    });
}

无论调起几次这个方法,打印只会进行一次

dispatch_apply ,重复执行, 如果任务队列是并行队列,重复执行的任务会并发执行,如果任务队列为串行队列,则任务会顺序执行,需要注意的是,该函数为同步函数,要防止线程阻塞和死锁

//重复执行
- (IBAction)recurButton:(id)sender {
    dispatch_apply(5, _dispatch_serial, ^(size_t i) {
        NSLog(@"重复执行的次数。    %ld",i);
    });
}

dispatch_after,延时执行,其中参数dispatch_time_t代表延时时长,dispatch_queue_t代表使用哪个队列。如果队列是主队列,那么任务在主线程执行,如果队列为全局队列或者自己创建的队列,那么任务在子线程执行

//延时执行
- (IBAction)afterButton:(id)sender {
    NSLog(@"延时三秒执行主线程--- 开始时间。%@",[NSDate date]);
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC));
    dispatch_after(time, dispatch_get_main_queue(), ^{
        NSLog(@"延时三秒执行主线程。%@",[NSDate date]);
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延时五秒执行主线程。%@",[NSDate date]);
    });
}

dispatch_group_async ,组队列 。dispatch_group_notify,接收组队列完成后执行的队列。当加入到队列组中的所有任务执行完成之后,会调用dispatch_group_notify函数通知任务全部完成

//分组完成
- (IBAction)groupButton:(id)sender {
    dispatch_group_t groupDispatch = dispatch_group_create();
    __block NSInteger index_1 = 0;
    __block NSInteger index_2 = 0;
    dispatch_group_async(groupDispatch, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         dispatch_apply(10000, _dispatch_serial, ^(size_t i) {
             index_1 = index_1 + i;
        });
    });
    dispatch_group_async(groupDispatch, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_apply(2000, _dispatch_serial, ^(size_t i) {
            index_2 = index_2 + i;
        });
    });
    dispatch_group_notify(groupDispatch, dispatch_get_main_queue(), ^{
        NSLog(@"这时候的 %ld,%ld",index_1,index_2);
    });
}

该文章练习的demo地址:demo

关闭

IT问道推荐

银行贷款频频被拒?
“Dr信用牛牛”让你远离信用污点 国内首家信用健康管理平台免费为你提供信用修复方案