美文网首页
iOS学习之GCD

iOS学习之GCD

作者: 龙马君 | 来源:发表于2016-03-28 22:12 被阅读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] 执行
    

    相关文章

      网友评论

          本文标题:iOS学习之GCD

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