網(wǎng)址:http://www.cnblogs.com/jy578154186/archive/2012/12/15/2819420.html
如何使用GCD?
GCD為Grand Central Dispatch的縮寫鸦泳〗缶冢 Grand Central Dispatch (GCD)是Apple開發(fā)的一個(gè)多核編程的較新的解決方法类咧。在Mac OS X 10.6雪豹中首次推出,并在最近引入到了iOS4.0芍碧。 GCD是一個(gè)替代諸如NSThread等技術(shù)的很高效和強(qiáng)大的技術(shù)号俐。GCD完全可以處理諸如數(shù)據(jù)鎖定和資源泄漏等復(fù)雜的異步編程問題泌豆。 GCD可以完成很多事情吏饿,但是這里僅關(guān)注在iOS應(yīng)用中實(shí)現(xiàn)多線程所需的一些基礎(chǔ)知識(shí)踪危。 在開始之前找岖,需要理解是要提供給GCD隊(duì)列的是代碼塊陨倡,用于在系統(tǒng)或者用戶創(chuàng)建的的隊(duì)列上調(diào)度運(yùn)行⌒聿迹 聲明一個(gè)隊(duì)列 如下會(huì)返回一個(gè)用戶創(chuàng)建的隊(duì)列: dispatch_queue_t myQueue = dispatch_queue_create("com.iphonedevblog.post", NULL);其中兴革,第一個(gè)參數(shù)是標(biāo)識(shí)隊(duì)列的,第二個(gè)參數(shù)是用來定義隊(duì)列的參數(shù)(目前不支持,因此傳入NULL)杂曲∈ 執(zhí)行一個(gè)隊(duì)列 如下會(huì)異步執(zhí)行傳入的代碼: dispatch_async(myQueue, ^{ [self doSomething]; });其中,首先傳入之前創(chuàng)建的隊(duì)列擎勘,然后提供由隊(duì)列運(yùn)行的代碼塊咱揍。 聲明并執(zhí)行一個(gè)隊(duì)列 如果不需要保留要運(yùn)行的隊(duì)列的引用棚饵,可以通過如下代碼實(shí)現(xiàn)之前的功能: dispatch_async(dispatch_queue_create ("com.iphonedevblog.post", NULL), ^{ [self doSomething]; }); 如果需要暫停一個(gè)隊(duì)列煤裙,可以調(diào)用如下代碼。暫停一個(gè)隊(duì)列會(huì)阻止和該隊(duì)列相關(guān)的所有代碼運(yùn)行噪漾∨鹋椋 dispatch_suspend(myQueue);暫停一個(gè)隊(duì)列 如果暫停一個(gè)隊(duì)列不要忘記恢復(fù)。暫停和恢復(fù)的操作和內(nèi)存管理中的retain和release類似欣硼。調(diào)用dispatch_suspend會(huì)增加暫停計(jì)數(shù)题翰,而dispatch_resume則會(huì)減少。隊(duì)列只有在暫停計(jì)數(shù)變成零的情況下才開始運(yùn)行诈胜。dispatch_resume(myQueue);恢復(fù)一個(gè)隊(duì)列 從隊(duì)列中在主線程運(yùn)行代碼 有些操作無法在異步隊(duì)列運(yùn)行豹障,因此必須在主線程(每個(gè)應(yīng)用都有一個(gè))上運(yùn)行。UI繪圖以及任何對(duì)NSNotificationCenter的調(diào)用必須在主線程長進(jìn)行焦匈。在另一個(gè)隊(duì)列中訪問主線程并運(yùn)行代碼的示例如下: dispatch_sync(dispatch_get_main_queue(), ^{ [self dismissLoginWindow]; });注意血公,dispatch_suspend (以及dispatch_resume)在主線程上不起作用。使用GCD括授,可以讓你的程序不會(huì)失去響應(yīng). 多線程不容易使用坞笙,用了GCD,會(huì)讓它變得簡單荚虚。你無需專門進(jìn)行線程管理, 很棒薛夜!讓你的程序保持響應(yīng)的原則:1. 不要柱塞主線程 2. 把工作一到其他線程中做。3. 做完后更新主線程的UI.舉例說明:沒有GCD的代碼:- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {? // 在主線程調(diào)用版述。? DTweet *tw = [[DTweet alloc] initWithMsg:msg];? [tweets addTweet:tw display:YES];? tw.img = [imageCache getImgFromURL:url];//bottle neck? [tweets updateTweet:tw display:YES];? [tw release];}有GCD的代碼:- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {//在主線程調(diào)用梯澜。? DTweet *tw = [[DTweet alloc] initWithMsg:msg];? [tweets addTweet:tw display:YES];? dispatch_async(image_queue, ^{? ? tw.img = [imageCache getImgFromURL:url];//放到一個(gè)異步隊(duì)列里。? ? dispatch_async(main_queue, ^{? ? ? [tweets updateTweet:tw display:YES];//放到異步的主線程里渴析。? ? });? });? [tw release];} 1. GCD is part of libSystem.dylib2. #include
一》
NSThread的方法:代碼如下:
- (void)viewDidLoad
{
[super viewDidLoad];
NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(print1) object:nil];
[thread1 start];
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(print2) object:nil];
[thread2 start];
}
-(void)print1{
for (int i=0; i<100; i++) {
NSLog(@"我是print1正在執(zhí)行%d",i);
}
}
-(void)print2{
for (int i=0; i<100; i++) {
NSLog(@"print2正在執(zhí)行%d",i);
}
}
二》
NSInvocationOperation
的方法:代碼如下
//? ? NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print1) object:@"1"];
//? ? NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print2) object:@"2"];//當(dāng)然這里可以用一個(gè)方法晚伙。
//? ? NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//? ? [queue addOperation:operation1];
//? ? [queue addOperation:operation2];
三》
GCD
的方法:代碼如下
dispatch_queue_t t1=dispatch_queue_create("1", NULL);
dispatch_queue_t t2=dispatch_queue_create("2", NULL);
dispatch_async(t1, ^{
[self print1];
});
dispatch_async(t2, ^{
[self print2];
});