1.什么是GCD
Grand Central Dispatch
好用:
简单,基于block C函数接口
功能强大, 支持多核心, 支持高级线程功能
2.什么是队列和任务
GCD中有2个核心概念
(1)任务:执行什么操作
(2)队列:用来存放任务
GCD的使用就2个步骤
(1)定制任务
(2)确定想做的事情
将任务添加到队列中,GCD会自动将队列中的任务取出,放到对应的线程中执行
提示:任务的取出遵循队列的FIFO原则:先进先出,后进后出
2.创建和使用
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self createAndUse]; } - (void)createAndUse { //开启新线程 //queue :队列 //三种 main_queue主线程队列 // globle_queue全局队列,异步任务加在这里 // 自定义de dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ for (int i=0; i<100; i++) { NSLog(@"A = %d----%@",i,[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i=0; i<100; i++) { NSLog(@"B = %d------%@",i,[NSThread currentThread]); } }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
3.模拟网络中的使用
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong)UIProgressView *progressView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self simulaterNetworkDataDownLoad]; } - (void)simulaterNetworkDataDownLoad { self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 100, 300, 20)]; [self.view addSubview:self.progressView]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for (int i =0; i<100; i++) { //注意: 不要在子线程中更新UI //self.progressView.progress += 0.01; //主线程中执行 dispatch_async(dispatch_get_main_queue(), ^{ self.progressView.progress += 0.01; }); [NSThread sleepForTimeInterval:0.1]; } NSLog(@"下载完成"); }); } - (void)createAndUse { } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
4.dispatch_after和dispatch_once_t
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //延时执行,延时5s执行 [self delayRunCode]; //有的代码指向执行一次 [self runOnceCode]; [self runOnceCode]; [self runOnceCode]; } - (void)delayRunCode { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"纸上得来终觉浅,绝知此事要躬行"); }); } - (void)runOnceCode { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"一次就好 我带你去看天荒地老"); }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
5.模拟多任务
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //模拟迅雷多任务下载 [self simulaterThunderDonwload]; } -(void)simulaterThunderDonwload { //任务群组 dispatch_group_t group = dispatch_group_create(); //任务群组中添加任务 5s完成 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for (int i=0; i<50; i++) { NSLog(@"A = %d",i); [NSThread sleepForTimeInterval:0.1]; } }); //任务群组中添加任务 3s完成 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for (int i=0; i<30; i++) { NSLog(@"B = %d",i); [NSThread sleepForTimeInterval:0.1]; } }); //任务群组中添加任务 10s完成 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for (int i=0; i<100; i++) { NSLog(@"C = %d",i); [NSThread sleepForTimeInterval:0.1]; } }); //监视所有任务执行完成 dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"所有任务执行完成"); }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
原文:http://www.cnblogs.com/CY-miaomiako/p/5112789.html