GCD

作者: 90后的思维 | 来源:发表于2018-03-26 11:20 被阅读16次

    GCD

    基础知识

    串行与并行

    串行队列:需要要等待上一个执行完,再执行下一个的Serial Dispatch Queue

    并行队列:不需要上一个执行完,就能执行下一个的Concurrent Dispatch Queue。这两种,均遵循FIFO原则。

    同步与异步

    串行与并行针对的是队列,而同步与异步,针对的则是线程。最大的区别在于,同步线程要阻塞当前线程,必须要等待同步线程中的任务执行完,返回以后,才能继续执行下一任务;而异步线程则是不用等待,各个线程同时执行。

    1.GCD的优势

    1)易用: GCD比之thread跟简单易用。由于GCD基于work unit而非像thread那样基于运算,所以GCD可以控制诸如等待任务结束、监视文件描述符、周期执行代码以及工作挂起等任务。基于block的血统导致它能极为简单得在不同代码作用域之间传递上下文。

    2)效率: GCD被实现得如此轻量和优雅,使得它在很多地方比之专门创建消耗资源的线程更实用且快速。这关系到易用性:导致GCD易用的原因有一部分在于你可以不用担心太多的效率问题而仅仅使用它就行了。

    3)性能: GCD自动根据系统负载来增减线程数量,这就减少了上下午切换以及增加了计算效率。

    2.GCD的种类

    1)系统队列

    main_queue

    dispatch_queue_tserialqueue = dispatch_get_main_queue();dispatch_async(serialqueue, ^{NSLog(@"main_queue");    });

    global_queue

    dispatch_queue_tqueue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);dispatch_async(queue, ^{            NSLog(@"global_queue");        });

    默认优先级的全局并发dispatch queue有三个优先级,分别是:

    DISPATCH_QUEUE_PRIORITY_HIGH 2

    DISPATCH_QUEUE_PRIORITY_DEFAULT 0

    DISPATCH_QUEUE_PRIORITY_LOW (-2)

    2)自己创建的队列

    serialQueue

    dispatch_queue_tmySerialQueue = dispatch_queue_create("com.example.mySerialQueue", DISPATCH_QUEUE_SERIAL);dispatch_async(mySerialQueue, ^{NSLog(@"mySerialQueue");    });dispatch_queue_tmySerialQueue = dispatch_queue_create("com.example.mySerialQueue",NULL);dispatch_async(mySerialQueue, ^{NSLog(@"mySerialQueue");    });

    concurrentQueue

    dispatch_queue_tmyConcurrentQueue = dispatch_queue_create("com.example.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);dispatch_async(myConcurrentQueue, ^{NSLog(@"myConcurrentQueue");    });

    在串行队列中,如果要在某项任务执行完后再执行其他任务,只需把该任务放到最后即可,但是在并行队列中怎么实现?可以使用Dispatch Groups 或者  Dispatch Semaphore

    1)Dispatch Groups

    Dispatch Groups 可以是用在并行队列中等某些任务完成后再执行其他任务

    dispatch_queue_tconcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);    dispatch_group_t group = dispatch_group_create();        dispatch_group_async(group, concurrentQueue, ^{        [NSThreadsleepForTimeInterval:6];NSLog(@"[NSThread sleepForTimeInterval:6];");    });        dispatch_group_async(group, concurrentQueue, ^{        [NSThreadsleepForTimeInterval:3];NSLog(@"[NSThread sleepForTimeInterval:3];");    });        dispatch_group_async(group, concurrentQueue, ^{        [NSThreadsleepForTimeInterval:1];NSLog(@"[NSThread sleepForTimeInterval:1];");    });        dispatch_group_notify(group, dispatch_get_main_queue(), ^{NSLog(@"finish");    });

    2)Dispatch Semaphore

    信号量一般用来做同步任务和有限资源访问控制

    dispatch_semaphore_tsemaphore = dispatch_semaphore_create(0);//创建信号量dispatch_semaphore_signal(semaphore);//发出信号量 让信号量加一 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//等待  信号量小于一则一直等待

    dispatch_semaphore_tsemaphore = dispatch_semaphore_create(0);            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0* NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{                dispatch_semaphore_signal(semaphore);    });        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    Dispatch_barrier

    dispatch_queue_tmyQueue = dispatch_queue_create("com.example.myQueue", DISPATCH_QUEUE_CONCURRENT);dispatch_async(myQueue, ^{        [NSThreadsleepForTimeInterval:3];NSLog(@"dispatch_async1");    });dispatch_async(myQueue, ^{        [NSThreadsleepForTimeInterval:1];NSLog(@"dispatch_async2");    });    dispatch_barrier_async(myQueue, ^{NSLog(@"dispatch_barrier_async");        [NSThreadsleepForTimeInterval:0.5];            });dispatch_async(myQueue, ^{        [NSThreadsleepForTimeInterval:1];NSLog(@"dispatch_async3");    });

    结果是 在并行队列中dispatch_barrier_async前面的正常执行,而他会阻塞后面的代码执行,直到前面的任务完成后,开始执行dispatch_barrier_async里面的代码 完成后才开始进行执行下去2016-04-0717:18:02.475GCD[2030:586717]dispatch_async22016-04-0717:18:04.473GCD[2030:586715]dispatch_async12016-04-0717:18:04.474GCD[2030:586717]dispatch_barrier_async2016-04-0717:18:05.981GCD[2030:586715]dispatch_async3

    常见问题#

    1)线程死锁

    dispatch_queue_tqueue = dispatch_queue_create("com.example.serialQueue", DISPATCH_QUEUE_SERIAL);dispatch_async(queue, ^{NSLog(@"sync  %@",[NSThreadcurrentThread]);dispatch_sync(queue, ^{NSLog(@"sync1  %@",[NSThreadcurrentThread]);        });dispatch_async(queue, ^{NSLog(@"async2  %@",[NSThreadcurrentThread]);        });    });

    dispatch_sync(dispatch_get_main_queue(), ^{NSLog(@"死锁");    });

    这两个阻塞的原因是 在 dispatch_sync 中,当前的队列和提交的队列相同,从而造成死锁。

    官方文档:

    Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

    参考文章

    https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/doc/uid/TP40008079

    相关文章

      网友评论

          本文标题:GCD

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