关于同步和异步(二)
2017-03-27 本文已影响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