1.GCD是最簡單的一種多線程耘擂,同時也是效率最高的一種方式(全部是用C語言代碼編寫的API)服傍,也是蘋果過公司主推的一種多線程方式
2.GCD通過queue來實(shí)現(xiàn)多線程
3.GCD里面有多種queue一種是串行serial一種是并行concurrent
pragma mark-------------serial串行隊(duì)列第一種------------------------
//serial:第一個任務(wù)執(zhí)行完畢,第二個任務(wù)才開始執(zhí)行,依次類推
//有兩種方式
//獲取主線程
// dispatch_queue_t queue = dispatch_get_main_queue();
//往隊(duì)列里面添加任務(wù)
// dispatch_async(queue, ^{
// NSLog(@"這是第一個任務(wù),當(dāng)前線程%@,是否主線%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);
// });
//
//
// dispatch_async(queue, ^{
// NSLog(@"這是第二個任務(wù)稍刀,當(dāng)前線程%@,是否主線程 %d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });
//
//
// dispatch_async(queue, ^{
// NSLog(@"這是第三個任務(wù)當(dāng)前線程%@ 敞曹,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });
//
// dispatch_async(queue, ^{
// NSLog(@"這是第四個第四個任務(wù)但錢線程%@账月,是否主線程 %d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });
pragma mark -------------serial串行隊(duì)列第二種-----------
/*
*獲取串行隊(duì)列的第二種方式,自己創(chuàng)建隊(duì)列澳迫。
*穿件串行隊(duì)列的第二種方式
“serialQueue” 對列的名字 (蘋果主推使用反向域名法去命名)
DISPATCH_QUEUE_SERIAL 隊(duì)列類型
手動創(chuàng)建的串行隊(duì)列不在主線程中
*/
//dispatch_sync 是主線程 dispatch_async不是主線程
dispatch_queue_t queue = dispatch_queue_create("com.serialQueue.www", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"這是一個任務(wù)局齿,當(dāng)前 線程%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"這是第二個任務(wù)纲刀,但錢線程%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"這是3%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"只是第四個%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"這是5個任務(wù)项炼,當(dāng)前 線程%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"這是第6個任務(wù)示绊,但錢線程%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"這是7%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"只是第8個%@,是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
concurrentQueue 特點(diǎn):第一個任務(wù)執(zhí)行開始之后锭部,第二個任務(wù)不等第一個執(zhí)行完畢直接開始執(zhí)行,以此類推面褐,后邊的任務(wù)跟前面的任務(wù)沒有關(guān)系(先添加的任務(wù)不一定先執(zhí)行拌禾,最后添加的任務(wù)不一定最后執(zhí)行),并列隊(duì)列會根據(jù)隊(duì)列里面的任務(wù)數(shù)量展哭、cpu湃窍、使用情況開辟最合適的線程數(shù)量去完成隊(duì)列里面的任務(wù) 創(chuàng)建有兩種方式
pragma mark ------concurrentQueue第一種創(chuàng)建方式-------------
// 創(chuàng)建并行隊(duì)列
//global queue 是蘋果里面的全局隊(duì)列,有4個優(yōu)先級
// #define DISPATCH_QUEUE_PRIORITY_HIGH 2
// #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
// #define DISPATCH_QUEUE_PRIORITY_LOW (-2)
// #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
//第一個參數(shù)就是隊(duì)列的優(yōu)先級 第二個參數(shù)就是蘋果預(yù)留的參數(shù)為了以后使用匪傍,目前還沒有使
// dispatch_queue_t queue = dispatch_queue_create("com.concurrent.www", DISPATCH_QUEUE_CONCURRENT);
//
// dispatch_async(queue, ^{
// NSLog(@"1%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });
//
// dispatch_async(queue, ^{
// NSLog(@"2%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);
// });
//
// dispatch_async(queue, ^{
// NSLog(@"3%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });
//
// dispatch_async(queue, ^{
// NSLog(@"4%@ 是否主%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
// });
pragma mark ------concurrentQueue第二種創(chuàng)建方式-------------
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSLog(@"線程1%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"線程2%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"線程3%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"線程4%@ 是否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"線程5%@ 誰否主%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
延遲執(zhí)行代碼 (dispatch——after 可以再任何隊(duì)列中執(zhí)行您市,串行并行都可以)
//第一種
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"我是延遲執(zhí)行的代碼 線程:%@ 是否是主線程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);
// });
//第二種
dispatch_time_t seconds = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));
//創(chuàng)建一個全局隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_after(seconds, queue,^{
NSLog(@"我是延遲執(zhí)行的代碼%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread ]);
});
線程組
//dispatch—Group-t主要是一些不想管的任務(wù)歸為一組
//組里面放到是隊(duì)列
//dispatch-group-async 作用是給組里面的隊(duì)列添加任務(wù)
//dispatch-group-notify 作用是堅(jiān)挺組里面的任務(wù),等到組里面的任務(wù)全部執(zhí)行完成之后役衡,才執(zhí)行它里面的任務(wù)
//第一步創(chuàng)建組
dispatch_group_t group = dispatch_group_create();
//創(chuàng)建全局對列
dispatch_queue_t queue = dispatch_queue_create(0, 0);
//往組里面的隊(duì)列添加任務(wù)
dispatch_group_async(group, queue, ^{
NSLog(@"我是第一個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_notify(group, queue, ^{
NSLog(@"我是最后一個任務(wù)茵休,當(dāng)其他任務(wù)完成之后,我再執(zhí)行 線程%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"我是第三個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"我是第四個%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"我是第五個%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(hroup, queue, ^{
NSLog(@"我是第五個%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
既有數(shù)據(jù)的寫入,也有數(shù)據(jù)的讀取榕莺。如何解決該類問題dispatch_barrier_async在它之前的任務(wù)可以去并發(fā)執(zhí)行俐芯,在它之后的任務(wù)也可以并發(fā)執(zhí)行
dispatch_queue_t queue = dispatch_queue_create("com.currentQueue.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"第一個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第二個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第三個任務(wù)%@ 是否主線成%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第四個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第5個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"我正在讀取數(shù)據(jù),不要來打擾我%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第6個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"第7個任務(wù)%@ 是否主線程%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
多次執(zhí)行
NSArray *array = @[@"1",@"2",@"3"];
dispatch_queue_t queue = dispatch_queue_create(0, 0);
/***
*多次執(zhí)行
*
*@param iterations 次數(shù)
*@param queue 隊(duì)列
*
*@param size_t 任務(wù)
*
*
***/
//index 是隨機(jī)的 是小于次數(shù)的隨機(jī)數(shù)
dispatch_apply(3, queue, ^(size_t index) {
NSLog(@"%@",array[index]);
});
async 與 sync的區(qū)別
//async 不等block執(zhí)行完畢钉鸯,就去執(zhí)行下面的代碼
//sync 會等block執(zhí)行完畢只后吧史,才會去執(zhí)行下面的代碼
dispatch_queue_t queue = dispatch_queue_create(0, 0);
dispatch_sync(queue, ^{
NSLog(@"這是第一個任務(wù)");
});
NSLog(@"呵呵");
dispatch_sync(queue, ^{
NSLog(@"這是第二個任務(wù)");
});
NSLog(@"哦");
GCD調(diào)用函數(shù)指針
- (IBAction)functionPointer:(UIButton *)sender {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
/***
*GCD 函數(shù)做參數(shù)
*queue 隊(duì)列
*contex 函數(shù)參數(shù)的內(nèi)容
*work 函數(shù)(函數(shù)肚餓返回值必須為void 參數(shù)類型必須為 void *)
***/
dispatch_async_f(queue, @"呵呵", function);
}
void function(void *context){
NSLog(@"%@",context);
printf("哦");
}
線程互斥
線程互斥是指某一資源同時只允許一個訪問者對其進(jìn)行訪問,具有唯一性和排它性唠雕。但互斥無法限制訪問者對資源的訪問順序贸营,即訪問是無序的
// 1 創(chuàng)建線程鎖
NSLock *lock = [[NSLock alloc]init];
_count = 100;//票數(shù)
//創(chuàng)建線程并行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("com.sellTicketss.www", DISPATCH_QUEUE_CONCURRENT);
__weak ViewController *weakSelf = self;
for (int i =10; i>0; i--) {
dispatch_async(queue, ^{
[lock lock];
for (int j = 10; j > 0; j--) {
NSLog(@"買到了第%d張票",weakSelf.count);
weakSelf.count -- ;
}
[lock unlock];
});
}