dispatch_apply and dispatch_barrier_async

 //自动开启线程数目控制,最优不会过多崩溃

    dispatch_apply(100, dispatch_get_global_queue(0, 0), ^(size_t index) {

        NSLog(@"%ld--%@", index,[NSThread currentThread]);

    });



dispatch_barrier_async  不会阻塞当前线程,等队列中任务都运行完在运行自己,比自己后加入的等自己运行完才能运行。

dispatch_barrier_sync 会阻塞当前线程一起等待,知道barr内运行完。


dispatch_queue_t queue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);



    dispatch_async(queue, ^{

        [selfdownLoad];

        NSLog(@"1111Finish,%@",[NSThread currentThread]);

    });


    dispatch_async(queue, ^{

        [selfdownLoad];

        NSLog(@"2222Finish,%@",[NSThread currentThread]);

    });



    dispatch_barrier_sync(queue, ^{

        [selfdownLoad];

        NSLog(@"BarrierFinish,%@",[NSThread currentThread]);

    });


    dispatch_async(queue, ^{

        NSLog(@"3333Finish,%@",[NSThread currentThread]);


    });



    NSLog(@"End");

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容