GCD全稱 Grand Central Dispatch ;
是IOS中對多線程處理的方法之一(其他NSThread,NSOperationQueue,NSInvocationOperation)
IOS系統對GCD的的使用方法大概有以下五種;
1.主線程執行
dispatch_async(dispatch_get_main_queue(), ^{
});
2.後台執行
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
3.延遲執行
double deleInseconds = 4.0;
/*
延遲更新 要用秒 乘以 NSEC_PER_SEC 宏定義如下
#define NSEC_PER_SEC 1000000000ull
#define NSEC_PER_MSEC 1000000ull
#define USEC_PER_SEC 1000000ull
#define NSEC_PER_USEC 1000ull
*/
dispatch_time_t poptime = dispatch_time(DISPATCH_TIME_NOW, deleInseconds * NSEC_PER_SEC);
dispatch_after(poptime, dispatch_get_main_queue(), ^{
});
4.只執行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
5. 並行執行,並匯總
dispatch_group_t group = dispatch_group_create();
//並行執行線程一
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"1000====%@",[NSDate date]);
sleep(100);
string = @"10";
});
//並行執行線程二
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
number = @"5";
NSLog(@"5=======%@",[NSDate date]);
});
//匯總結果
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"------------- %f",string.floatValue + number.floatValue);
});
具體代碼參考github地址 :
https://github.com/wang6177ming123/GCDBlock
【二十五,iOS GCD的使用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!