并行、串行揍异、異步全陨、同步的加深理解
單個(gè)理解:
并行:就是隊(duì)列里面的任務(wù)(代碼塊,block)不是一個(gè)個(gè)執(zhí)行衷掷,而是同時(shí)執(zhí)行辱姨,不存在先后順序
串行:隊(duì)列里面的任務(wù)一個(gè)接著一個(gè)執(zhí)行,第一個(gè)執(zhí)行完了才能輪到第二個(gè)
異步:具有新開線程的能力
同步:不具備有新開線程的能力棍鳖,只能在當(dāng)前線程執(zhí)行任務(wù)(所以任務(wù)只能一個(gè)挨著一個(gè)執(zhí)行炮叶,而不能同時(shí)進(jìn)行)
串起來理解:
并行+異步:就是真正的并發(fā),新開有有多個(gè)線程處理任務(wù)渡处,任務(wù)并發(fā)執(zhí)行(不按順序執(zhí)行)
串行+異步:新開一個(gè)線程镜悉,任務(wù)一個(gè)接一個(gè)執(zhí)行,上一個(gè)任務(wù)處理完畢医瘫,下一個(gè)任務(wù)才可以被執(zhí)行
并行+同步:不新開線程侣肄,任務(wù)一個(gè)接一個(gè)執(zhí)行
串行+同步:不新開線程,任務(wù)一個(gè)接一個(gè)執(zhí)行
? 這里不得不說一下醇份,之前看了這篇博客
http://www.cnblogs.com/ziyi--caolu/p/4900650.html 該作者成功的將我繞進(jìn)了里面稼锅,其實(shí)歸根究底,只要是在同步環(huán)境中僚纷,并行和串行確實(shí)就可以這么理解矩距,
即并行+同步 和 串行+同步 都是不新開線程,并且任務(wù)一個(gè)接一個(gè)執(zhí)行
下面是具體代碼示例:
一:異步并行隊(duì)列內(nèi)回調(diào)同步串行隊(duì)列
- (void)exampleFirst {
dispatch_queue_t queue = dispatch_queue_create("com.hao123.www", DISPATCH_QUEUE_SERIAL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---11 %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
//打印結(jié)果與主隊(duì)列bingxingyibuqiantaozhu函數(shù)保持一致
NSLog(@"11 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---22 %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"22 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---33 %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"33 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---44 %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"44 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---55 %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"55 %@",[NSThread currentThread]);
});
});
}
打印結(jié)果
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978456] 外部---44 <NSThread: 0x608000072780>{number = 6, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978364] 外部---11 <NSThread: 0x608000070f40>{number = 3, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978366] 外部---33 <NSThread: 0x600000076380>{number = 5, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978363] 外部---22 <NSThread: 0x600000072f00>{number = 4, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978457] 外部---55 <NSThread: 0x608000073340>{number = 7, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978456] 44 <NSThread: 0x608000072780>{number = 6, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978364] 11 <NSThread: 0x608000070f40>{number = 3, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978366] 33 <NSThread: 0x600000076380>{number = 5, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978363] 22 <NSThread: 0x600000072f00>{number = 4, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978457] 55 <NSThread: 0x608000073340>{number = 7, name = (null)}
二:異步并行內(nèi)回調(diào)同步并行隊(duì)列
- (void)exampleSecond {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---11 %@",[NSThread currentThread]);
//對(duì)比異步并行回調(diào)同步串行怖竭,可以看出當(dāng)外部異步并行隊(duì)列未執(zhí)行完畢時(shí)锥债,有可能內(nèi)部同步并行隊(duì)列已經(jīng)開始執(zhí)行了
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"11 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---22 %@",[NSThread currentThread]);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"22 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---33 %@",[NSThread currentThread]);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"33 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---44 %@",[NSThread currentThread]);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"44 %@",[NSThread currentThread]);
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"外部---55 %@",[NSThread currentThread]);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"55 %@",[NSThread currentThread]);
});
});
}
打印結(jié)果
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984344] 外部---11 <NSThread: 0x608000263040>{number = 13, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984353] 外部---22 <NSThread: 0x608000262480>{number = 17, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984111] 外部---33 <NSThread: 0x608000073980>{number = 12, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984345] 外部---55 <NSThread: 0x60000007f1c0>{number = 16, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984346] 外部---44 <NSThread: 0x608000074d40>{number = 14, name = (null)}
2017-09-08 14:34:59.193 GcdInsightTest[64242:2984344] 11 <NSThread: 0x608000263040>{number = 13, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984353] 22 <NSThread: 0x608000262480>{number = 17, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984111] 33 <NSThread: 0x608000073980>{number = 12, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984345] 55 <NSThread: 0x60000007f1c0>{number = 16, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984346] 44 <NSThread: 0x608000074d40>{number = 14, name = (null)}
根據(jù)兩段例子的打印結(jié)果,可以顯而易見的得出結(jié)論:
一:由于外部是異步并行隊(duì)列痊臭,所以外部打印結(jié)果不按順序執(zhí)行哮肚。
二:當(dāng)外部所有隊(duì)列執(zhí)行完畢,內(nèi)部隊(duì)列才開始執(zhí)行广匙。
三:其內(nèi)部的隊(duì)列將與外部隊(duì)列順序保持一致允趟,無論異步并行隊(duì)列內(nèi)回調(diào)的是同步串行隊(duì)列,還是同步并行隊(duì)列鸦致,只有當(dāng)外部隊(duì)列執(zhí)行完畢潮剪,接下來內(nèi)部隊(duì)列才會(huì)在同一個(gè)線程繼續(xù)執(zhí)行直到任務(wù)結(jié)束涣楷。
根據(jù)第三條可以得出結(jié)論:
并行+同步:不新開線程,任務(wù)一個(gè)接一個(gè)執(zhí)行
串行+同步:不新開線程鲁纠,任務(wù)一個(gè)接一個(gè)執(zhí)行
這兩句話沒毛病~