美文网首页
iOS GCD笔记

iOS GCD笔记

作者: _秃头少女_ | 来源:发表于2019-04-08 14:55 被阅读0次

    1.概念

    a. 主队列是串行队列,只会有一条线程,顺序执行任务.
    b.全局队列是并行队列,会根据任务需要创建线程

    截屏2020-02-22下午6.50.30.png

    https://www.jianshu.com/p/2d57c72016c6

    dispatch_queue_t main = dispatch_get_main_queue();//获取主队列
    
    //#define DISPATCH_QUEUE_PRIORITY_HIGH 2 高优先级
    //#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认优先级
    //#define DISPATCH_QUEUE_PRIORITY_LOW (-2) 低优先级
    //#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 后台优先级
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//获取全局队列 默认优先级
    
    1.主队列开启同步任务会造成死锁
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_queue_t main = dispatch_get_main_queue();
    
        dispatch_sync(main, ^{
            NSLog(@"%@",[NSThread currentThread]);
            NSLog(@"1111====");
        });
        NSLog(@"4444444====");
    
    }
    
    2.GCD定时器
       __block NSInteger count = 0 ;
        dispatch_queue_t global = dispatch_get_global_queue(0, 0);
        dispatch_source_t time = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);
        dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)3.0 * NSEC_PER_SEC);
        uint64_t interval = (uint64_t)(2.0* NSEC_PER_SEC);
        dispatch_source_set_timer(time, start, interval, 0);
        dispatch_source_set_event_handler(time, ^{
            count ++ ;
            NSLog(@"定时器====%ld",(long)count);
            
            if(count == 10){
                dispatch_cancel(time);
            }
        });
        dispatch_resume(time);
    

    log:

    2019-03-28 17:43:08.762551+0800 GCD[35064:1408702] 定时器====1
    2019-03-28 17:43:10.762517+0800 GCD[35064:1408702] 定时器====2
    2019-03-28 17:43:12.762607+0800 GCD[35064:1408702] 定时器====3
    2019-03-28 17:43:14.762485+0800 GCD[35064:1408703] 定时器====4
    2019-03-28 17:43:16.762472+0800 GCD[35064:1408702] 定时器====5
    2019-03-28 17:43:18.762483+0800 GCD[35064:1408703] 定时器====6
    2019-03-28 17:43:20.762493+0800 GCD[35064:1408703] 定时器====7
    2019-03-28 17:43:22.762553+0800 GCD[35064:1408703] 定时器====8
    2019-03-28 17:43:24.762410+0800 GCD[35064:1408702] 定时器====9
    2019-03-28 17:43:26.762428+0800 GCD[35064:1408702] 定时器====10
    
    3.dispatch_barrier_sync
         dispatch_queue_t global = dispatch_queue_create("GCD", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(global, ^{
            NSLog(@"1=====%@",[NSThread currentThread]);
        });
        dispatch_async(global, ^{
            NSLog(@"2=====%@",[NSThread currentThread]);
        });
        dispatch_barrier_sync(global, ^{
            NSLog(@"4=dispatch_barrier_async====%@",[NSThread currentThread]);
        });
        dispatch_async(global, ^{
            NSLog(@"5=====%@",[NSThread currentThread]);
        });
        dispatch_async(global, ^{
            NSLog(@"6=====%@",[NSThread currentThread]);
        });
    

    dispatch_barrier_sync(queue,void(^block)())会将queue中barrier前面添加的任务block全部执行后,再执行barrier任务的block,再执行barrier后面添加的任务block.
    dispatch_barrier_async(queue,void(^block)())会将queue中barrier前面添加的任务block只添加不执行,继续添加barrier的block,再添加barrier后面的block,同时不影响主线程(或者操作添加任务的线程)中代码的执行!
    总而言之,dispatch_barrier_async是用于任务按序执行的!

    4.dispatch_group_t

    配合 dispatch_group_notify 使用,group执行完会统一回调,适用于必须多个任务先执行,执行结束再执行另一个任务

        // 创建一个并行的queue
        dispatch_queue_t queue = dispatch_queue_create("gcd.group", DISPATCH_QUEUE_CONCURRENT);
        // 创建group
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"star任务1");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"end任务1");
        });
        dispatch_group_async(group, queue, ^{
            NSLog(@"star任务2");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"end任务2");
        });
        dispatch_group_notify(group, queue, ^{
            NSLog(@"所有任务执行完毕的回调");
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"回到主线程刷新UI");
            });
        });
    
    5.GCD使用dispatch_semaphore_t控制并发数

    dispatch_semaphore_create(3)设置最大并发数为3 ,当程序执行dispatch_semaphore_wait 进行-1操作,为0其他任务继续等待,直到前一批任务执行dispatch_semaphore_signal 值+1,才会再有新的任务进入并继续执行dispatch_semaphore_wait

    dispatch_queue_t workConcurrentQueue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(3);
        
        for (NSInteger i = 0; i < 10; i++) {
            dispatch_async(workConcurrentQueue, ^{
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                dispatch_async(workConcurrentQueue, ^{
                    NSLog(@"thread-info:%@开始执行任务%d",[NSThread currentThread],(int)i);
                    sleep(1);
                    NSLog(@"thread-info:%@结束执行任务%d",[NSThread currentThread],(int)i);
                    dispatch_semaphore_signal(semaphore);});
            });
        }
        NSLog(@"主线程...!");
    

    6,主队列中添加异步任务,不会开启新线程,全部在主线程顺序执行

        dispatch_queue_t queue = dispatch_queue_create("chuanxing", DISPATCH_QUEUE_SERIAL);
        dispatch_queue_t mainqueue = dispatch_get_main_queue();
        dispatch_async(mainqueue, ^{
            NSLog(@"11111======%@",[NSThread currentThread]);
        });
        dispatch_async(mainqueue, ^{
            NSLog(@"33333======%@",[NSThread currentThread]);
        });
        dispatch_async(mainqueue, ^{
            NSLog(@"55555======%@",[NSThread currentThread]);
        });
        NSLog(@"2==========%@",[NSThread currentThread]);
    

    log:

    2020-03-01 13:15:23.444035+0800 11[26828:12241023] 2==========<NSThread: 0x600001ac2d40>{number = 1, name = main}
    2020-03-01 13:15:23.481389+0800 11[26828:12241023] 11111======<NSThread: 0x600001ac2d40>{number = 1, name = main}
    2020-03-01 13:15:23.481560+0800 11[26828:12241023] 33333======<NSThread: 0x600001ac2d40>{number = 1, name = main}
    2020-03-01 13:15:23.481688+0800 11[26828:12241023] 55555======<NSThread: 0x600001ac2d40>{number = 1, name = main}
    

    https://blog.csdn.net/jeffasd/article/details/51519919
    https://www.jianshu.com/p/f59a497a913e

    相关文章

      网友评论

          本文标题:iOS GCD笔记

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