iOS学习iOS学习笔记iOS Developer

GCD的使用

2016-11-15  本文已影响47人  云海长天

GCD的详解
iOS多线程--彻底学会多线程之『GCD』

GCD线程之间的通讯

一般在主线程里面刷新UI

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    for (int i = 0; i < 2; ++i) { 
        NSLog(@"1------%@",[NSThread currentThread]);
    } 
    // 回到主线程 
    dispatch_async(dispatch_get_main_queue(), ^{ 
        NSLog(@"2-------%@",[NSThread currentThread]);     
    });
});

输出结果:
2016-09-03 19:34:59.165 GCD[11728:1913039] 1------<NSThread: 0x7f8319c06820>{number = 2, name = (null)}
2016-09-03 19:34:59.166 GCD[11728:1913039] 1------<NSThread: 0x7f8319c06820>{number = 2, name = (null)}
2016-09-03 19:34:59.166 GCD[11728:1912961] 2-------<NSThread: 0x7f8319e00560>{number = 1, name = main}

GCD的栅栏方法dispatch_barrier_async

dispatch_queue_t queue = dispatch_queue_create("123456", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        
        NSLog(@"----1----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----2----%@",[NSThread currentThread]);
        
    });
    
    dispatch_barrier_async(queue, ^{
        
        NSLog(@"----barrier----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----3----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----4----%@",[NSThread currentThread]);
        
    });

输出结果:
2016-09-03 19:35:51.271 GCD[11750:1914724] ----1-----<NSThread: 0x7fb1826047b0>{number = 2, name = (null)}
2016-09-03 19:35:51.272 GCD[11750:1914722] ----2-----<NSThread: 0x7fb182423fd0>{number = 3, name = (null)}
2016-09-03 19:35:51.272 GCD[11750:1914722] ----barrier-----<NSThread: 0x7fb182423fd0>{number = 3, name = (null)}
2016-09-03 19:35:51.273 GCD[11750:1914722] ----3-----<NSThread: 0x7fb182423fd0>{number = 3, name = (null)}
2016-09-03 19:35:51.273 GCD[11750:1914724] ----4-----<NSThread: 0x7fb1826047b0>{number = 2, name = (null)}

GCD的延时执行方法dispatch_after

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //  2秒后异步执行这里的代码
        
        NSLog(@"run----");
        
    });

GCD的一次性代码(只执行一次)dispatch_once

static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //  只执行一次的代码(这里默认是线程安全的)
    });

GCD的快速迭代方法dispatch_apply

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_apply(6, queue, ^(size_t index) {
        
        NSLog(@"%zd----%@",index,[NSThread currentThread]);
        
    });

输出结果:
2016-09-03 19:37:02.250 GCD[11764:1915764] 1------<NSThread: 0x7fac9a7029e0>{number = 1, name = main}
2016-09-03 19:37:02.250 GCD[11764:1915885] 0------<NSThread: 0x7fac9a614bd0>{number = 2, name = (null)}
2016-09-03 19:37:02.250 GCD[11764:1915886] 2------<NSThread: 0x7fac9a542b20>{number = 3, name = (null)}
2016-09-03 19:37:02.251 GCD[11764:1915764] 4------<NSThread: 0x7fac9a7029e0>{number = 1, name = main}
2016-09-03 19:37:02.250 GCD[11764:1915884] 3------<NSThread: 0x7fac9a76ca10>{number = 4, name = (null)}
2016-09-03 19:37:02.251 GCD[11764:1915885] 5------<NSThread: 0x7fac9a614bd0>{number = 2, name = (null)}

GCD的队列组dispatch_group

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //  执行一个耗时操作的异步操作
        
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //  执行一个耗时操作的异步操作
        
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        //  等待前面的异步操作都执行完毕后,回到主线程
        
    });
上一篇 下一篇

猜你喜欢

热点阅读