趁著最近的項(xiàng)目不多的空檔,將《Objective-C高級(jí)編程:iOS與OS X多線程和內(nèi)存管理》這本業(yè)內(nèi)大神推薦的書給閱讀一遍,主要從底層解析ARC內(nèi)存管理 Block 以及GCD,就GCD寫一遍總結(jié)文章.
線程到底是什么?
int main()
{
id o = [myobject alloc] init];
[o execBlock];
return 0;
}
代碼基本都是從上到下的順序執(zhí)行的,實(shí)際上這段代碼會(huì)通過(guò)編譯器轉(zhuǎn)換成CPU命令列(二進(jìn)制代碼)
一個(gè)CPU只能執(zhí)行一個(gè)命令,只能等這條命令執(zhí)行完后再執(zhí)行下一條,所以"一個(gè)CPU執(zhí)行的CPU命令列為一天無(wú)分叉路徑",即為線程,而一臺(tái)計(jì)算機(jī)使用多個(gè)CPU就會(huì)有多條無(wú)分叉路徑即為"多線程"
Dispatch Queue
dispatch_async(queue,^{
//想執(zhí)行的任務(wù)
});
代碼使用Block語(yǔ)法"定義想執(zhí)行的任務(wù)",通過(guò)dispatch_async函數(shù)追加到賦值變量queue的"Dispatch Queue"中,這樣就能使指定的Block在另一個(gè)線程中執(zhí)行
添加任務(wù)的隊(duì)列 Dispatch Queue分為兩種:
Serial Dispatch Queue 等待現(xiàn)在執(zhí)行中的處理結(jié)束
放在該隊(duì)列中的兩個(gè)任務(wù)A和B,在同一條線程中,先執(zhí)行A時(shí),只有等A執(zhí)行完畢后才會(huì)執(zhí)行B
dispatch_queue_t serialqueue = dispatch_queue_create("bunldid serialQueue",NULL);
Concurrent Dispatch Queue 不等待現(xiàn)在執(zhí)行中的處理結(jié)束
放在改隊(duì)列中的兩人任務(wù)A和B,在執(zhí)行A時(shí)不會(huì)等A執(zhí)行完,會(huì)在另一條線程中執(zhí)行
dispatch_queue_t concurrentlqueue = dispatch_queue_create("bunldid serialQueue",DISPATCH_QUEUE_CONCURRENT);
創(chuàng)建的全局隊(duì)列就是系統(tǒng)提供的不等待執(zhí)行的concurrent dispatch queue
dispatch_queue_t globalqueue = dispatch_get_global_queue(0,0);
全局隊(duì)列存在優(yōu)先級(jí)
dispatch_set_target_queue(用于手動(dòng)設(shè)置隊(duì)列執(zhí)行的優(yōu)先級(jí))
dispatch_queue_t serialqueue = dispatch_queue_create("bunlad id", NULL);
dispatch_queue_t globalqueue = dispatch_queue_create("bulnd id",DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(serialqueue, globalqueue);
這樣serialqueue的優(yōu)先級(jí)就高于globalqueue
dispatch_after(延時(shí)執(zhí)行)
指定時(shí)間追加處理到Dispatch Queue
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW,3ull * NSEC_PER_SEC);
dispatch_after(time,dispatch_get_main_queue(),^{
//延時(shí)3秒執(zhí)行
});
Dispatch Group
用于多個(gè)任務(wù)完成后再進(jìn)行下下一個(gè)任務(wù)的操作
dispatch_queue_t queue = dispatch_get_blobal_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,queue,^{ NSLog(@"balo1"); });
dispatch_group_async(group,queue,^{ NSLog(@"balo2"); });
dispatch_group_async(group,queue,^{ NSLog(@"balo2"); });
待三個(gè)任務(wù)完成后執(zhí)行下面的任務(wù)
dispatch_group_notify(group,dispatch_get_main_queue(),^{
//最后執(zhí)行的任務(wù)
});
dispatch_barrier_async
在訪問(wèn)數(shù)據(jù)庫(kù)或文件時(shí),使用Serial Dispatch Queue可避免數(shù)據(jù)競(jìng)爭(zhēng)的問(wèn)題,為了高效率的進(jìn)行訪問(wèn),讀取處理時(shí)追加到Concurrent Dispatch Queue中進(jìn)行,寫入處理追加到Serial Dispatch Queue中進(jìn)行處理
如果有多個(gè)concurrent dispatch queue進(jìn)行多次的數(shù)據(jù)讀取
只需要?jiǎng)?chuàng)建一個(gè)concurrent dispatch queue
dispatch_queue_t queue = dispatch_queue_create("bunld id",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
當(dāng)其中需要進(jìn)行一部數(shù)據(jù)寫入的操作時(shí),可以使用Dispatch_barrier_async來(lái)進(jìn)行操作,即使使用的不等待隊(duì)列Concurrent Queue,仍然會(huì)等待這個(gè)任務(wù)完成后恢復(fù)不等待屬性
dispatch_queue_t queue = dispatch_queue_create("bunld id",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
dispatch_barrier_async(queue,^{
//進(jìn)行數(shù)據(jù)寫入
});
dispatch_async(queue,^{
//進(jìn)行數(shù)據(jù)讀取
});
dispatch_apply
dispatch_apply函數(shù)是diapatch_sync函數(shù)和dispatch_group關(guān)聯(lián)的API,這個(gè)函數(shù)是按照指定的次數(shù)將指定的Block追加到指定的Dispatch_Queue中,并等待全部執(zhí)行結(jié)束后再執(zhí)行下一步操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_apply(10,queue,^(size_t index) {
});
dispatch_apply函數(shù)中的參數(shù):第一個(gè)是重復(fù)的次數(shù),第二個(gè)是追加對(duì)象的Dispatch Queue,第三個(gè)帶有Block參數(shù)的是重復(fù)追加的操作,可以用來(lái)代替for循環(huán)操作
以上就是這本書大概的對(duì)于GCD的介紹,不對(duì)之處,請(qǐng)多指教