- (void)cast1{
//1.用异步函数,往全局并发队列添加任务。异步并发开辟新线程同时运行
//参数:1.优先级<#long identifier#> 2.为将来做准备,通常设为0<#unsigned long flags#>
//串行:dispatch_queue_create("1234567", NULL);
//获取全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//并发
//异步
//把右边的操作(block)交个左边的队列(queue)
dispatch_async(queue, ^{
//添加任务
NSLog(@"下载1---- %@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
//添加任务
NSLog(@"下载2---- %@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
//添加任务
NSLog(@"下载3---- %@",[NSThreadcurrentThread]);
});dispatch_async(queue, ^{
//添加任务
NSLog(@"下载4---- %@",[NSThreadcurrentThread]);
});
NSLog(@"当前线程---- %@",[NSThreadcurrentThread]);
//总结:异步函数具有开启线程的能力----异步并发执行四个操作(开启了四个分线程)
}
- (void)cast7 {
//异步并发
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL* url = [NSURLURLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
NSData* data = [NSDatadataWithContentsOfURL:url];
// NSLog(@"---- %@",data);
__blockUIImage* image = [UIImageimageWithData:data];
NSLog(@"----%@",[NSThreadcurrentThread]);
//回到主线程刷新UI
// 1. [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];
// 2. [self.image1 performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
// 3. [self performSelector:@selector(settingImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
// 4. [self.image1 performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
//回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"currentThread ---- %@",[NSThreadcurrentThread]);
self.image1.image= image;
});
//优点:分线程中得到的数据,可以直接使用,不需要传值,更加方便和直接
});
}
- (void)settingImage:(UIImage *)image{
self.image1.image= image;
}
- (void)cast8{
//异步并发
dispatch_async(GLOBAL_QUEUE, ^{
__block UIImage* mage1 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
__block UIImage* mage2 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
//在主线程刷新UI dispatch_get_main_queue
dispatch_async(dispatch_get_main_queue(), ^{
self.image1.image= mage1;
self.image2.image= mage2;
//合成图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100),NO, 0.0);
[mage1drawInRect:CGRectMake(0, 0, 100, 100)];
[mage2drawInRect:CGRectMake(100, 0, 100, 100)];
self.image3.image=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
NSLog(@"currentThread -- %@",[NSThreadcurrentThread]);
});
NSLog(@"111currentThread -- %@",[NSThreadcurrentThread]);
});
}
- (UIImage*)imageWithURL:(NSString*)URLStr{
NSURL* url = [NSURLURLWithString:URLStr];
NSData* data = [NSDatadataWithContentsOfURL:url];
UIImage* image = [UIImageimageWithData:data];
return image;
}
- (void)cast2{
//2.用同步函数往全局并发队列中添加任务同步并发在同一个线程中同时运行
//获取全局并发队列
//总结:用同步函数往全局并发队列中添加任务,由于同步函数不具有开辟线程的能力,所以我们的操作在当前线程串行执行
}
- (void)cast3{
//3.用同步函数往串行队列中添加任务同步串行在同一个线程中一个一个运行任务
//获取全局串行队列
//1.队列的名字(方便测试) 2.队列的属性,一般为NULL
dispatch_queue_t queue =dispatch_queue_create("123456",NULL);
dispatch_sync(queue, ^{
NSLog(@"下载1---- %@",[NSThreadcurrentThread]);
});
//在当前线程中串行执行
}
- (void)cast4 {
//常用
//4.用异步函数往串行队中添加任务异步串行开辟另一条线程一个一个运行任务(只另外开辟一条)
// CONCURRENT并发DISPATCH_QUEUE_SERIAL串行
//总结
}
- (void)cast9 {
//异步串行
//同时执行下载两张图片
//队列组
//创建一个队列组
dispatch_group_tgroup =dispatch_group_create();
__blockUIImage* im1 =nil;
//将block(操作)添加到全局并发队列中,然后全局并发队列添加到队列组中
dispatch_group_async(group,GLOBAL_QUEUE, ^{
im1 = [selfimageWithURL:@"http://p3.pstatp.com/large/9e00004286008bddc3c"];
NSLog(@"currentThread --- %@",[NSThreadcurrentThread]);
});
__blockUIImage* im2 =nil;
//将block(操作)添加到全局并发队列中,然后全局并发队列添加到队列组中
dispatch_group_async(group,GLOBAL_QUEUE, ^{
im2 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
NSLog(@"currentThread --- %@",[NSThreadcurrentThread]);
});
//队列组中的所有操作执行完调用该方法---回到主线程刷新UI
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
self.image1.image= im1;
self.image2.image= im2;
//合成图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100),NO, 0.0);
[im1drawInRect:CGRectMake(0, 0, 100, 100)];
[im2drawInRect:CGRectMake(100, 0, 100, 100)];
self.image3.image=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
NSLog(@"currentThread -- %@",[NSThreadcurrentThread]);
});
}
- (void)cast5 {
//5.用异步函数往主队列中添加任务主队列异步串行
//获取主队列
dispatch_queue_tqueue =dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"下载1---- %@",[NSThreadcurrentThread]);
});
//总结:放在主队列中的任务,都在主线程中串行执行分线程管回调
}
- (void)cast6{
//6.在主线程中用同步函数往主队列中添加任务主队列同步
//卡死无输出
//获取主队列
dispatch_queue_t queue =dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"下载1---- %@",[NSThreadcurrentThread]);
});
//总结:在主线程中用同步函数往主队列中添加任务会造成主线程死锁下面的代码不会执行
}
//在分线程中用同步函数往主队列中添加任务---不会造成死锁
[self performSelectorInBackground:@selector(cast6) withObject:nil];
- (void)cast10 {
//延迟函数
//1.NSObjiect的延迟方法
NSLog(@"111111.currentThread --- %@",[NSThreadcurrentThread]);
[selfperformSelector:@selector(run)withObject:nilafterDelay:2.0];
//2.GCD
//什么时间在哪个队列中执行什么方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"1.currentThread --- %@",[NSThreadcurrentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"3.currentThread --- %@",[NSThreadcurrentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_queue_create("123",NULL), ^{
NSLog(@"2.currentThread --- %@",[NSThreadcurrentThread]);
});
}
- (void)run {
NSLog(@"0.currentThread --- %@",[NSThreadcurrentThread]);
}
- (void)cast11{
//dispatch_once函数中的操作只执行一次单列类
for(inti = 0; i < 20;i ++) {
__block int count = 0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
count ++;
NSLog(@"我执行了%d次",count);
});
}
}
宏定义方式
//1.GLOBAL_QUEUE
//2.kPrivate_Global_Queue
//3.kGlobal_Queue
#define GLOBAL_QUEUE dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define kAfteTime(x) (int64_t) (x * NSEC_PER_SEC)