// // MainViewController.m // GCD // // Created by hejin on 13-12-29. // Copyright (c) 2013年 e世雕龍. All rights reserved. // #import "MainViewController.h" @interface MainViewController () @property (strong, nonatomic) IBOutlet UIProgressView *myProgress;//進度條 @property (strong, nonatomic) IBOutlet UITextView *textView;//多行文本框 @end @implementation MainViewController //GCD方式更新進度條 - (IBAction)updateProgress:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ for (int i=0; i<100; i++) { [NSThread sleepForTimeInterval:0.02]; dispatch_async(dispatch_get_main_queue(), ^{ self.myProgress.progress += 0.01; }); } }); } //GCD並發多線程完成任務 - (IBAction)asynDowork:(id)sender { self.textView.text = @""; NSMutableString *strWork = [NSMutableString stringWithCapacity:20]; //不使用多線程 /*[strWork appendFormat:@"%@\n",[self doWork1]]; [strWork appendFormat:@"%@\n",[self doWork2]]; [strWork appendFormat:@"%@\n",[self doWork3]]; self.textView.text = strWork;*/ //獲取已經存在並始終可用的全局隊列 dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0); //獲取主線程隊列 dispatch_queue_t mainQueue = dispatch_get_main_queue(); //並發多線程完成任務 dispatch_async(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork1]]; NSLog(@"doWork1"); dispatch_async(mainQueue, ^{ self.textView.text = strWork; NSLog(@"updateUI"); }); }); dispatch_async(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork2]]; NSLog(@"doWork2"); dispatch_async(mainQueue, ^{ self.textView.text = strWork; NSLog(@"updateUI"); }); }); dispatch_async(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork3]]; NSLog(@"doWork3"); dispatch_async(mainQueue, ^{ self.textView.text = strWork; NSLog(@"updateUI"); }); }); } //GCD同步完成任務 - (IBAction)synDowork:(id)sender { self.textView.text = @""; NSMutableString *strWork = [NSMutableString stringWithCapacity:20]; //獲取已經存在並始終可用的全局隊列 dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0); //獲取主線程隊列 dispatch_queue_t mainQueue = dispatch_get_main_queue(); //同步完成任務 dispatch_sync(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork1]]; NSLog(@"doWork1"); }); dispatch_sync(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork2]]; NSLog(@"doWork2"); }); dispatch_sync(globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork3]]; NSLog(@"doWork3"); }); //在主線程中更新UI,如下代碼同步等待主線程中按鈕點擊事件完成才進行,主線程中按鈕點擊事件等待如下代碼完成,造成阻塞 /*dispatch_sync(globalQueue, ^{ dispatch_sync(mainQueue, ^{ self.textView.text = strWork; }); });*/ dispatch_async(globalQueue, ^{ dispatch_async(mainQueue, ^{ self.textView.text = strWork; }); }); } //使用GCD推薦的分組派發同步完成任務 - (IBAction)groupSynDowork:(id)sender { self.textView.text = @""; NSMutableString *strWork = [NSMutableString stringWithCapacity:20]; //創建組和隊列 dispatch_group_t group = dispatch_group_create(); dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0); //隊列在組中執行操作 dispatch_group_async(group, globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork1]]; NSLog(@"doWork1"); }); dispatch_group_async(group, globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork2]]; NSLog(@"doWork2"); }); dispatch_group_async(group, globalQueue, ^{ [strWork appendFormat:@"%@\n",[self doWork3]]; NSLog(@"doWork3"); }); //通知組中任務完成在主線程中更新UI dispatch_group_notify(group, dispatch_get_main_queue(), ^{ self.textView.text = strWork; }); } //任務1 - (NSString *)doWork1 { [NSThread sleepForTimeInterval:2]; return @"任務1完成!"; } //任務2 - (NSString *)doWork2 { [NSThread sleepForTimeInterval:3]; return @"任務2完成!"; } //任務3 - (NSString *)doWork3 { [NSThread sleepForTimeInterval:4]; return @"任務3完成!"; } @end