关于同步和异步(二)

#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");
}
关于同步和异步(二)_第1张图片
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");
}
关于同步和异步(二)_第2张图片
Snip20170327_14.png

你可能感兴趣的:(关于同步和异步(二))