iOS学习之GCD

2016-03-28  本文已影响25人  龙马君
    //创建并行队列
    dispatch_queue_t queue1 = dispatch_queue_create("com.longma.xzm.queue1", DISPATCH_QUEUE_CONCURRENT);
    // 创建串行队列
    dispatch_queue_t queue2 = dispatch_queue_create("com.longma.xzm.queue2", DISPATCH_QUEUE_SERIAL);
    
    // 异步执行
    dispatch_async(queue1, ^(){
        for (int i = 0; i < 5; i++) {
            NSLog(@"++++++++");
        }
    });
    
    dispatch_async(queue2, ^(){
        for (int i = 0; i < 5; i++) {
            NSLog(@"----------");
        }
    });
    
    dispatch_async(queue2, ^(){
        for (int i = 0; i < 5; i++) {
            NSLog(@"==========");
        }
    });


    NSLog(@"3s后执行");
    dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC);
    dispatch_after(t, queue1, ^{
        NSLog(@"执行");
    });

执行结果:

2016-03-28 22:11:38.764 MyiOSLearn[978:17819] 3s后执行
2016-03-28 22:11:38.764 MyiOSLearn[978:17886] ++++++++
2016-03-28 22:11:38.764 MyiOSLearn[978:17887] ----------
2016-03-28 22:11:38.765 MyiOSLearn[978:17886] ++++++++
2016-03-28 22:11:38.773 MyiOSLearn[978:17887] ----------
2016-03-28 22:11:38.774 MyiOSLearn[978:17886] ++++++++
2016-03-28 22:11:38.775 MyiOSLearn[978:17887] ----------
2016-03-28 22:11:38.775 MyiOSLearn[978:17886] ++++++++
2016-03-28 22:11:38.776 MyiOSLearn[978:17887] ----------
2016-03-28 22:11:38.776 MyiOSLearn[978:17886] ++++++++
2016-03-28 22:11:38.776 MyiOSLearn[978:17887] ----------
2016-03-28 22:11:38.778 MyiOSLearn[978:17887] ==========
2016-03-28 22:11:38.778 MyiOSLearn[978:17887] ==========
2016-03-28 22:11:38.779 MyiOSLearn[978:17887] ==========
2016-03-28 22:11:38.780 MyiOSLearn[978:17887] ==========
2016-03-28 22:11:38.780 MyiOSLearn[978:17887] ==========
2016-03-28 22:11:41.969 MyiOSLearn[978:17887] 执行
上一篇下一篇

猜你喜欢

热点阅读