一才漆、簡(jiǎn)介
蘋(píng)果官方對(duì)GCD是這樣說(shuō)明的:開(kāi)發(fā)者要做的只是定義想執(zhí)行的任務(wù)并追加到適當(dāng)?shù)腄ispatch Queue中恕曲。Dispatch Queue是執(zhí)行處理的等待隊(duì)列褥影,我們可以通過(guò)dispatch_async等API,在block語(yǔ)法中記述想要執(zhí)行的處理并將其追加到Dispatch Queue中离例,Dispatch Queue是按照追加的順序 進(jìn)行處理换团,先進(jìn)先出FIFO。
用代碼表示就是:
dispatch_async(queue,^{
//想執(zhí)行的任務(wù)宫蛆,這樣執(zhí)行就是在另一個(gè)新開(kāi)辟的線程中
});
在執(zhí)行處理時(shí)候是有兩種Dispatch Queue艘包,一種是Serial Dispatch Queue串行調(diào)度隊(duì)列,這個(gè)是等待現(xiàn)在執(zhí)行中的事件處理結(jié)束耀盗,另一種是Concurrent Dispatch Queue并發(fā)調(diào)度隊(duì)列,這個(gè)是不等待現(xiàn)在執(zhí)行中的事件處理結(jié)束想虎。
異步、同步叛拷、并行舌厨、串行的特點(diǎn)
異步執(zhí)行:具備開(kāi)啟新線程的能力,任務(wù)創(chuàng)建后可以先繞過(guò)忿薇,回頭再執(zhí)行邓线。
同步執(zhí)行:不具備開(kāi)啟新線程的能力,任務(wù)創(chuàng)建后就要執(zhí)行完才能繼續(xù)往下走煌恢。
并行隊(duì)列:隊(duì)列中的任務(wù)同時(shí)執(zhí)行骇陈。
串行隊(duì)列:隊(duì)列中的任務(wù)要按順序執(zhí)行。
二瑰抵、使用
1.異步執(zhí)行?并行隊(duì)列
//1.異步執(zhí)行+并行隊(duì)列
- (void)asyncConcurrent{
//創(chuàng)建一個(gè)并行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---%@",[NSThread currentThread]);
//使用異步函數(shù)封裝三個(gè)任務(wù)你雌,三個(gè)任務(wù)同時(shí)執(zhí)行,不一定是按任務(wù)順序打印 可先繞過(guò)不執(zhí)行
dispatch_async(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@",[NSThread currentThread]);
}
2.異步執(zhí)行?串行隊(duì)列
- (void)asyncSerial{
//創(chuàng)建一個(gè)串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_SERIAL);
NSLog(@"---start---%@",[NSThread currentThread]);
//使用異步函數(shù)封裝三個(gè)任務(wù) 可先繞過(guò)不執(zhí)行 任務(wù)必須按添加進(jìn)隊(duì)列的順序挨個(gè)執(zhí)行
dispatch_async(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@",[NSThread currentThread]);
}
3.同步執(zhí)行?并行隊(duì)列
- (void)syncConcurrent{
//創(chuàng)建一個(gè)并行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---%@",[NSThread currentThread]);
//使用同步函數(shù)封裝三個(gè)任務(wù)
dispatch_sync(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@",[NSThread currentThread]);
}
4.同步執(zhí)行+ 串行隊(duì)列
(void)syncSerial{
//創(chuàng)建一個(gè)串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_SERIAL);
NSLog(@"---start---%@", [NSThread currentThread]);
//使用異步函數(shù)封裝三個(gè)任務(wù)
dispatch_sync(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@", [NSThread currentThread]);
}
只要是同步執(zhí)行就沒(méi)法開(kāi)啟新的線程二汛,所以多個(gè)任務(wù)之間也一樣只能按順序來(lái)執(zhí)行婿崭。
5、異步執(zhí)行+主隊(duì)列
- (void)asyncMain{
//獲取主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"---start---%@", [NSThread currentThread]);
//使用異步函數(shù)封裝三個(gè)任務(wù) 所有任務(wù)都可以先跳過(guò)肴颊,之后再來(lái)“按順序”執(zhí)行
dispatch_async(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@", [NSThread currentThread]);
}
6.同步執(zhí)行+主隊(duì)列(死鎖)
- (void)syncMain{
//獲取主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"---start---%@", [NSThread currentThread]);
//使用同步函數(shù)封裝三個(gè)任務(wù)
dispatch_sync(queue, ^{
NSLog(@"任務(wù)1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任務(wù)3---%@", [NSThread currentThread]);
});
NSLog(@"---end---%@", [NSThread currentThread]);
}
原因:
主隊(duì)列中的任務(wù)必須按順序挨個(gè)執(zhí)行
任務(wù)1要等主線程有空的時(shí)候(即主隊(duì)列中的所有任務(wù)執(zhí)行完)才能執(zhí)行
主線程要執(zhí)行完“打印end”的任務(wù)后才有空
“任務(wù)1”和“打印end”兩個(gè)任務(wù)互相等待氓栈,造成死鎖