dispatch_sync() 和 dispatch_async() 这两个方法区别

dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"1");

dispatch_async(concurrentQueue, ^(){  

    NSLog(@"2");

[NSThread sleepForTimeInterval:5];

NSLog(@"3");});

NSLog(@"4");

输出为:

2016-08-2511:50:51.601xxxx[1353:102804]12016-08-2511:50:51.601xxxx[1353:102804]22016-08-2511:50:56.603xxxx[1353:102804]32016-08-2511:50:56.603xxxx[1353:102804]4

再看:

dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"1");

dispatch_async(concurrentQueue, ^(){   

    NSLog(@"2");

[NSThread sleepForTimeInterval:5];

NSLog(@"3");});

NSLog(@"4");

输出为:

2016-08-2511:52:29.022xxxx[1392:104246]12016-08-2511:52:29.023xxxx[1392:104246]42016-08-2511:52:29.023xxxx[1392:104284]22016-08-2511:52:34.029xxxx[1392:104284]3

通过上边的两个例子,我们可以总结出:

dispatch_sync(),同步添加操作。他是等待添加进队列里面的操作完成之后再继续执行。

dispatch_async ,异步添加进任务队列,它不会做任何等待

你可能感兴趣的:(dispatch_sync() 和 dispatch_async() 这两个方法区别)