同步和異步:
同步:囂張跋扈漩勤,立刻馬上執(zhí)行笙以,如果我沒執(zhí)行完淌实,大家都別執(zhí)行
異步:謙虛禮讓,我沒有執(zhí)行猖腕,其他人可以執(zhí)行
1)同步 + 并發(fā):不開線程拆祈,串行執(zhí)行(同步函數(shù)霸道,執(zhí)行完start倘感,必須把同步函數(shù)的任務(wù)執(zhí)行完放坏,再執(zhí)行end)。
NSLog(@"----%@",[NSThread currentThread] );
//1,創(chuàng)建隊(duì)列
/*參數(shù)1老玛,C語(yǔ)言的字符串淤年,標(biāo)簽
參數(shù)2,隊(duì)列類型
DISPATCH_QUEUE_CONCURRENT 并發(fā)
DISPATCH_QUEUE_SERIAL 串行
*/
// dispatch_queue_t queue = dispatch_queue_create("fff", DISPATCH_QUEUE_CONCURRENT);
//獲得全局并發(fā)隊(duì)列逻炊,參數(shù)1互亮,優(yōu)先級(jí)
//1,獲取并發(fā)隊(duì)列,
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSLog(@"start");
//2,同步函數(shù)把任務(wù)添加到隊(duì)列
dispatch_sync(queue, ^{
NSLog(@"download1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3---%@",[NSThread currentThread]);
});
NSLog(@"end");
Snip20170327_7.png
2)異步 + 并發(fā) :開多條線程余素,隊(duì)列任務(wù)并發(fā)執(zhí)行(異步函數(shù)謙讓,執(zhí)行完start炊昆,可以先執(zhí)行end桨吊,再執(zhí)行異步函數(shù)的任務(wù))
NSLog(@"----%@",[NSThread currentThread] );
//1,獲取并發(fā)隊(duì)列,
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSLog(@"start");
//2,異步函數(shù)把任務(wù)添加到隊(duì)列
dispatch_async(queue, ^{
NSLog(@"download1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3---%@",[NSThread currentThread]);
});
NSLog(@"end");
Snip20170327_8.png