美文网首页程序员
关于同步和异步(二)

关于同步和异步(二)

作者: 心情的蛊惑 | 来源:发表于2017-03-27 13:35 被阅读54次
    #pragma mark- 同步 + 串行 :不会开线程,串行执行
    -(void)syncChuan{
        NSLog(@"----%@",[NSThread currentThread] );
        //1,获取并发队列,
        dispatch_queue_t queue = dispatch_get_main_queue();
        NSLog(@"start");
        //2,同步函数把任务添加到队列
        dispatch_sync(queue, ^{
            NSLog(@"download1---%@",[NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"download2---%@",[NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"download3---%@",[NSThread currentThread]);
        });
        NSLog(@"end");
    }
    
    Snip20170327_13.png
    #pragma mark- 异步 + 串行 :会开一条线程,串行执行
    -(void)asyncChuan {
        NSLog(@"----%@",[NSThread currentThread] );
        //1,获取并发队列,
      dispatch_queue_t queue = dispatch_queue_create("fff", DISPATCH_QUEUE_SERIAL);
        NSLog(@"start");
        //2,异步函数把任务添加到队列
        dispatch_async(queue, ^{
            NSLog(@"download1---%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"download2---%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"download3---%@",[NSThread currentThread]);
        });
        NSLog(@"end");
    }
    
    Snip20170327_14.png

    相关文章

      网友评论

        本文标题:关于同步和异步(二)

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