GCD
GCD(GRAND CENTRAL DISPATCH),就是牛逼的中樞調(diào)度器。C語(yǔ)言寫的,提供了多種函數(shù)。
1.任務(wù)和隊(duì)列
GCD兩個(gè)核心的概念就是任務(wù)和隊(duì)列。 任務(wù):執(zhí)行什么操作的。 隊(duì)列:專門來存放任務(wù)的。 GCD的使用很簡(jiǎn)單,也就是先把任務(wù)定制好,然后放在隊(duì)列中,GCD會(huì)自動(dòng)的將任務(wù)從隊(duì)列中取出來,然后放到對(duì)應(yīng)的線程中去執(zhí)行,任務(wù)的取出遵循先進(jìn)先出和后進(jìn)后出的原則。
2.同步、異步
// 同步 dispatch_sync(<
// 異步
dispatch_async(<
queue:隊(duì)列
block:任務(wù)
復(fù)制代碼同步和異步的區(qū)別: 同步:只能在當(dāng)前線程中執(zhí)行任務(wù),不具備開啟新的線程的能力 異步:可以在新的線程中執(zhí)行任務(wù),具備開啟新的線程的能力
3.串行隊(duì)列、并發(fā)隊(duì)列
并發(fā)隊(duì)列:可以將多個(gè)任務(wù)并發(fā)的同時(shí)的執(zhí)行,自動(dòng)的開啟多個(gè)線程同時(shí)執(zhí)行任務(wù)。 串行隊(duì)列:任務(wù)是一個(gè)接著一個(gè)的執(zhí)行。
以上的四個(gè)術(shù)語(yǔ)的一些說明:關(guān)于同步異步,只是說明了有無開具新的線程的能力;而對(duì)串行和并發(fā)隊(duì)列來說就是任務(wù)執(zhí)行的方式。
4.GCD的基本使用
/**異步+并發(fā)->會(huì)開啟多條線程的,隊(duì)列中任務(wù)是異步執(zhí)行的*/
- (void)wjAsyncConcurrent {// 1.創(chuàng)建隊(duì)列// DISPATCH_QUEUE_CONCURRENT : 并發(fā)隊(duì)列dispatch_queue_t queue = dispatch_queue_create(
"com.wj.downLoad", DISPATCH_QUEUE_CONCURRENT);// 2.定制任務(wù)->還將把任務(wù)添加到隊(duì)列中dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 1 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 2 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 3 : %@", [NSThread currentThread]);});/*打印結(jié)果異步+并發(fā) 1 : <NSThread: 0x600000070480>{number = 3, name = (null)}異步+并發(fā) 3 : <NSThread: 0x600000070840>{number = 4, name = (null)}異步+并發(fā) 2 : <NSThread: 0x600000070680>{number = 5, name = (null)}*/
}
復(fù)制代碼/**異步+串行->會(huì)開線程,開了一條線程,隊(duì)列中的任務(wù)是串行執(zhí)行的。*/
- (void)wjAsyncSerial {dispatch_queue_t queue = dispatch_queue_create(
"com.wj.download", DISPATCH_QUEUE_SERIAL);dispatch_async(queue, ^{NSLog(@
"異步+串行 1 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+串行 2 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+串行 3 : %@", [NSThread currentThread]);});/*打印結(jié)果異步+串行 1 : <NSThread: 0x60800006dd80>{number = 3, name = (null)}異步+串行 2 : <NSThread: 0x60800006dd80>{number = 3, name = (null)}異步+串行 3 : <NSThread: 0x60800006dd80>{number = 3, name = (null)}*/
}
復(fù)制代碼/**同步+并發(fā)->不會(huì)開線程,在主線程中執(zhí)行,且任務(wù)是串行執(zhí)行*/
- (void)wjSyncConcurrent {dispatch_queue_t queue = dispatch_queue_create(
"com.wj.downLoad", DISPATCH_QUEUE_CONCURRENT);dispatch_sync(queue, ^{NSLog(@
"同步+并發(fā) 1 : %@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@
"同步+并發(fā) 2 : %@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@
"同步+并發(fā) 3 : %@", [NSThread currentThread]);});/*打印結(jié)果同步+并發(fā) 1 : <NSThread: 0x608000261f80>{number = 1, name = main}同步+并發(fā) 2 : <NSThread: 0x608000261f80>{number = 1, name = main} 同步+并發(fā) 3 : <NSThread: 0x608000261f80>{number = 1, name = main}*/
}
復(fù)制代碼/**同步+串行->不會(huì)開線程*/
- (void)wjSyncSerial {dispatch_queue_t queue = dispatch_queue_create(
"com.wj.download", DISPATCH_QUEUE_SERIAL);dispatch_sync(queue, ^{NSLog(@
"同步+串行 1 : %@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@
"同步+串行 2 : %@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@
"同步+串行 3 : %@", [NSThread currentThread]);});/*打印結(jié)果同步+串行 1 : <NSThread: 0x600000077180>{number = 1, name = main}同步+串行 2 : <NSThread: 0x600000077180>{number = 1, name = main}同步+串行 3 : <NSThread: 0x600000077180>{number = 1, name = main}*/
}
復(fù)制代碼/**獲得全局并發(fā)隊(duì)列*/
- (void)wjGetGlobleConcurrentQueue {dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 1 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 2 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 3 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 4 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 5 : %@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@
"異步+并發(fā) 6 : %@", [NSThread currentThread]);});/*打印結(jié)果->可以顯示出,并不是有多少個(gè)任務(wù)就創(chuàng)建多少個(gè)線程,而是有系統(tǒng)自己決定的。異步+并發(fā) 3 : <NSThread: 0x60000026c700>{number = 5, name = (null)}異步+并發(fā) 4 : <NSThread: 0x6080002688c0>{number = 6, name = (null)}異步+并發(fā) 1 : <NSThread: 0x608000268c80>{number = 3, name = (null)}異步+并發(fā) 2 : <NSThread: 0x608000268a80>{number = 4, name = (null)}異步+并發(fā) 5 : <NSThread: 0x60000026c3c0>{number = 7, name = (null)}異步+并發(fā) 6 : <NSThread: 0x60000026c700>{number = 5, name = (null)}*/
}
復(fù)制代碼
總結(jié)
以上是生活随笔為你收集整理的iOS之多线程 2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。