-(void)group1
{
//1.创建队列
dispatch_queue_tqueue =dispatch_get_global_queue(0,0);
//2.创建队列组
dispatch_group_tgroup =dispatch_group_create();
//3.异步函数
/*
1)封装任务
2)把任务添加到队列中
dispatch_async(queue, ^{
NSLog(@"1----%@",[NSThread currentThread]);
});
*/
/*
1)封装任务
2)把任务添加到队列中
3)会监听任务的执行情况,通知group
*/
dispatch_group_async(group, queue, ^{
NSLog(@"1----%@",[NSThreadcurrentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"2----%@",[NSThreadcurrentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"3----%@",[NSThreadcurrentThread]);
});
//拦截通知,当队列组中所有的任务都执行完毕的时候回进入到下面的方法
dispatch_group_notify(group, queue, ^{
NSLog(@"-------dispatch_group_notify-------");
});
//NSLog(@"----end----");
}
-(void)group2
{
//1.创建队列
dispatch_queue_tqueue =dispatch_get_global_queue(0,0);
//2.创建队列组
dispatch_group_tgroup =dispatch_group_create();
//3.在该方法后面的异步任务会被纳入到队列组的监听范围,进入群组
//dispatch_group_enter|dispatch_group_leave必须要配对使用
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"1----%@",[NSThreadcurrentThread]);
//离开群组
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"2----%@",[NSThreadcurrentThread]);
//离开群组
dispatch_group_leave(group);
});
//拦截通知
//问题?该方法是阻塞的吗?内部本身是异步的
//dispatch_group_notify(group, queue, ^{
//NSLog(@"-------dispatch_group_notify-------");
//});
//等待.死等.直到队列组中所有的任务都执行完毕之后才能执行
//阻塞的
dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
NSLog(@"----end----");
}
-(void)group3
{
/*
1.下载图片1开子线程
2.下载图片2开子线程
3.合成图片并显示图片开子线程
*/
//-1.获得队列组
dispatch_group_tgroup =dispatch_group_create();
//0.获得并发队列
dispatch_queue_tqueue =dispatch_get_global_queue(0,0);
// 1.下载图片1开子线程
dispatch_group_async(group, queue,^{
NSLog(@"download1---%@",[NSThreadcurrentThread]);
//1.1确定url
NSURL*url = [NSURLURLWithString:@"http://www.qbaobei.com/tuku/images/13.jpg"];
//1.2下载二进制数据
NSData*imageData = [NSDatadataWithContentsOfURL:url];
//1.3转换图片
self.image1= [UIImageimageWithData:imageData];
});
// 2.下载图片2开子线程
dispatch_group_async(group, queue,^{
NSLog(@"download2---%@",[NSThreadcurrentThread]);
//2.1确定url
NSURL*url = [NSURLURLWithString:@"http://pic1a.nipic.com/2008-09-19/2008919134941443_2.jpg"];
//2.2下载二进制数据
NSData*imageData = [NSDatadataWithContentsOfURL:url];
//2.3转换图片
self.image2= [UIImageimageWithData:imageData];
});
//3.合并图片
//主线程中执行
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
NSLog(@"combie---%@",[NSThreadcurrentThread]);
//3.1创建图形上下文
UIGraphicsBeginImageContext(CGSizeMake(200,200));
//3.2画图1
[self.image1drawInRect:CGRectMake(0,0,200,100)];
self.image1=nil;
//3.3画图2
[self.image2drawInRect:CGRectMake(0,100,200,100)];
self.image2=nil;
//3.4根据上下文得到一张图片
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
//3.5关闭上下文
UIGraphicsEndImageContext();
//3.6更新UI
//dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"UI----%@",[NSThreadcurrentThread]);
self.imageView.image= image;
//});
});
//dispatch_release(group)
}
-(void)test
{
// dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)
//区别:封装任务的方法(block--函数)
/*
第一个参数:队列
第二个参数:参数
第三个参数:要调用的函数的名称
*/
dispatch_async_f(dispatch_get_global_queue(0,0),NULL,task);
dispatch_async_f(dispatch_get_global_queue(0,0),NULL,task);
dispatch_async_f(dispatch_get_global_queue(0,0),NULL,task);
}
voidtask(void*param)
{
NSLog(@"%s---%@",__func__,[NSThreadcurrentThread]);
}