GCD
Grand Central Dispatch
GCD中也有一个类似于NSOperationQueue的队列,GCD统一管理整个队列中的任务。但是GCD中的队列分为并行队列和串行队列两类:
串行队列:只有一个线程,加入到队列中的操作按添加顺序依次执行。
串行队列:有多个线程,操作进来之后它会将这些队列安排在可用的处理器上,同时保证先进来的任务优先处理。
GCD中有2个用来执行任务的函数
只是根据队列的性质,分为<1>串行队列:用户队列、主线程队列 <2>并行队列.
说明:把右边的参数(任务)提交给左边的参数(队列)进行执行。
(1)用串行队列的方式执行任务 dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
参数说明:
queue:队列
block:任务
同步(dispatch_sync)、异步方式(dispatch_async). 配合串行队列和并行队列使用.
效果显示.
异步串行
异步并行
如果图挂了 点击超链接
同步
如果图挂了 点击超链接
我相信这里gif的展示令你更深刻认识到GCD.
其他任务执行方法
贴上案例
#import "ViewController.h"
@interface ViewController ()
@property (strong , nonatomic) NSMutableArray *imageView;
@property (strong,nonatomic) NSArray *imageNames;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//数组
NSString *Photo1 =[NSString stringWithFormat:@"http://news.mydrivers.com/img/20130518/3fed7f3e8ab848e284bb2238320cbd93.jpg"];
NSString *Photo2 =[NSString stringWithFormat:@"http://image.tianjimedia.com/uploadImages/2013/237/015W189B5F99_B75.JPG"];
NSString *Photo3 =[NSString stringWithFormat:@"http://pic.3h3.com/up/2014-6/20146614141118561372.jpg"];
NSString *Photo4 =[NSString stringWithFormat:@"http://www.3dmgame.com/uploads/allimg/130518/154_130518143149_1.jpg"];
NSString *Photo5 =[NSString stringWithFormat:@"http://b.hiphotos.baidu.com/zhidao/pic/item/cdbf6c81800a19d8a50020aa31fa828ba61e4619.jpg"];
NSString *Photo6 =[NSString stringWithFormat:@"http://img8.zol.com.cn/bbs/upload/23416/23415166.jpg"];
self.imageNames =[NSArray arrayWithObjects:
Photo1,Photo2,Photo3, Photo4,Photo5,Photo6,nil];
[self loadScrollViewWithButton];
}
-(void)loadimageViews{
//ImageView
_imageView=[NSMutableArray array];
for (int i = 0 ; i <6 ; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5 +(self.view.bounds.size.width/2 )*(i/3),(self.view.bounds.size.height/3 -25)*(i%3), self.view.bounds.size.width/2-10, self.view.bounds.size.width/2-10)];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
[_imageView addObject:imageView];
}
}
-(void)loadScrollViewWithButton{
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height -100, self.view.bounds.size.width, 40)];
scrollView.backgroundColor = [UIColor lightGrayColor]; scrollView.alpha = 0.6;
scrollView.contentSize = CGSizeMake(1000, 0);
[self.view addSubview:scrollView];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1000, 60)];
lable.text = @"scrollView测试滚动,加载时滚动条体验.图片加载是否在主线程中";
[scrollView addSubview:lable];
//串行队列
UIButton *TandemQueueButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
TandemQueueButton.frame=CGRectMake(0, self.view.bounds.size.height -60, 100,60);
[TandemQueueButton setTitle:@"异步串行队列" forState:UIControlStateNormal];
TandemQueueButton.backgroundColor =[UIColor grayColor];
TandemQueueButton.alpha = 0.8;
[TandemQueueButton addTarget:self action:@selector(loadAsyncTandemQueue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:TandemQueueButton];
//异步串行队列
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(110, self.view.bounds.size.height -60, 100,60);
[button setTitle:@"异步并行队列" forState:UIControlStateNormal];
button.backgroundColor =[UIColor grayColor];
button.alpha = 0.8;
[button addTarget:self action:@selector(loadAsyncParallelQueue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[TandemQueueButton addTarget:self action:@selector(loadAsyncTandemQueue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:TandemQueueButton];
//同步队列
UIButton *SynchronousButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
SynchronousButton.frame=CGRectMake(220, self.view.bounds.size.height -60, 100,60);
[SynchronousButton setTitle:@"同步队列" forState:UIControlStateNormal];
SynchronousButton.backgroundColor =[UIColor grayColor];
SynchronousButton.alpha = 0.8;
[SynchronousButton addTarget:self action:@selector(loadsyncParallelQueue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:SynchronousButton];
//计事件队列
UIButton *timeButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
timeButton.frame=CGRectMake(330, self.view.bounds.size.height -60, 100,60);
[timeButton setTitle:@"延迟" forState:UIControlStateNormal];
timeButton.backgroundColor =[UIColor grayColor];
timeButton.alpha = 0.8;
[timeButton addTarget:self action:@selector(loadTimeQueue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:timeButton];
}
#pragma mark 将图片显示到界面
-(void)updateImageWithData:(NSData *)data andIndex:(int )index{
UIImage *image=[UIImage imageWithData:data];
UIImageView *imageView= _imageView[index];
imageView.image=image;
}
#pragma mark 请求图片数据
-(NSData *)requestData:(int)index{
NSURL *url=[NSURL URLWithString:_imageNames[index]];
NSData *data=[NSData dataWithContentsOfURL:url];
return data;
}
//图片加载
-(void)loadImage:(NSNumber *)index{
int i = (int)[index integerValue];
NSData * data = [self requestData:i];
dispatch_queue_t mainQue = dispatch_get_main_queue() ;
//为什么要用async?
//async 在主线程中 创建了一个异步线程 加入 全局并发队列,async 不会等待block 执行完成,立即返回
// 1,async 立即返回, viewDidLoad 执行完毕,及主线程执行完毕。
//2,同时,全局并发队列立即执行异步 block , 打印 1, 当执行到 sync 它会等待 block 执行完成才返回, 及等待
//dispatch_get_main_queue() 队列中的 mianThread 执行完成, 然后才开始调用block 。
//体现一下卡顿 sync的卡顿. 他会在在updateImageWithData 卡顿了.因为没有返回;
// dispatch_async(mainQue, ^{
// [self updateImageWithData:data andIndex:i];
// });
dispatch_async(mainQue, ^{
[self updateImageWithData:data andIndex:i];
});
}
//清除子view
-(void)cleanimage{
[_imageView makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
//异步串行队列意思是一张张按顺序图片加载,
-(void)loadAsyncTandemQueue{
[self cleanimage];
[self loadimageViews];
int iamgecount = 6;
dispatch_queue_t serialQueue = dispatch_queue_create("myThreadQueue1", DISPATCH_QUEUE_SERIAL);//注意queue对象不是指针类型
//创建多个线程用于填充图片
for (int i=0; i<iamgecount; ++i) {
//异步执行队列任务
dispatch_async(serialQueue, ^{
[self loadImage:[NSNumber numberWithInt:i]];
});
}
}
//异步并行队列 不会按顺序加载
-(void)loadAsyncParallelQueue{
[self cleanimage];
[self loadimageViews];
int iamgecount = 6;
/*dispatch_get_global_queue(参数1, 参数2);
取得全局队列
第一个参数:线程优先级
第二个参数:标记参数,目前没有用,一般传入0
*/
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建多个线程用于填充图片
for (int i=0; i<iamgecount; ++i) {
//异步执行队列任务
dispatch_async(globalQueue, ^{
[self loadImage:[NSNumber numberWithInt:i]];
});
}
}
// GCD sync 卡顿.
// 内嵌套一个async 就可以运行起来.
//同步队列. 同步 无论并行还是串行 也是最后统一加载
//同时会占据主线程, 造成scrollView 不能拖动
-(void)loadsyncParallelQueue{
[self cleanimage];
[self loadimageViews];
int iamgecount = 6;
dispatch_queue_t syncQueue = dispatch_queue_create("myThreadQueue2", DISPATCH_QUEUE_SERIAL);
//创建多个线程用于填充图片
for (int i=0; i<iamgecount; ++i) {
//同步执行队列任务
dispatch_sync(syncQueue, ^{
NSLog(@"%@ i = %d",syncQueue,i);
[self loadImage:[NSNumber numberWithInt:i]];
});
}
}
-(void)loadTimeQueue{
[self cleanimage];
[self loadimageViews];
int iamgecount = 6;
dispatch_queue_t serialQueue = dispatch_queue_create("myThreadQueue1", DISPATCH_QUEUE_SERIAL);//注意queue对象不是指针类型
//创建多个线程用于填充图片
for (int i=0; i<iamgecount; ++i) {
//异步执行队列任务
dispatch_async(serialQueue, ^{
[self timeAfter];
[self loadImage:[NSNumber numberWithInt:i]];
});
}
}
-(void)timeAfter{
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 10);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"10秒后加载一行图片");
});
}
@end
GCD执行任务的方法并非只有简单的同步调用方法和异步调用方法,还有其他一些常用方法:
dispatch_apply():重复执行某个任务,但是注意这个方法没有办法异步执行(为了不阻塞线程可以使用dispatch_async()包装一下再执行)。
dispatch_once():单次执行一个任务,此方法中的任务只会执行一次,重复调用也没办法重复执行(单例模式中常用此方法)。
dispatch_time():延迟一定的时间后执行。
dispatch_group_async():实现对任务分组管理,如果一组任务全部完成可以通过dispatch_group_notify()方法获得完成通知(需要定义dispatch_group_t作为分组标识)。