GCD之阻塞死锁问题以及多个图片下载优化

对于程序猿来说,代码比空白文字来的有力多。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"看看当前线程:%@",[NSThread currentThread]);
    
    //[self Test1];
    
    //[self Test2];
    
    //[self Test3];
    
    [self Test4];
    
}

-(void)Test1
{
    //创建一个串行队列
    dispatch_queue_t queue=dispatch_queue_create("gujinyue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
    
    //    for(int i=0;i<10;i++)
    //    {
    //        dispatch_async(queue, ^{
    //
    //            NSLog(@"开启咯一个异步任务,%@ %d",[NSThread currentThread],i);
    //        });
    //    }
    
    //    for(int i=0;i<10;i++)
    //    {
    //        dispatch_sync(queue, ^{
    //
    //            NSLog(@"开启咯一个同步任务,%@ %d",[NSThread currentThread],i);
    //        });
    //    }
    //
    //    for(int i=0;i<10;i++)
    //    {
    //        dispatch_async(queue, ^{
    //
    //            NSLog(@"开启咯一个异步任务,%@ %d",[NSThread currentThread],i);
    //        });
    //    }
    
    dispatch_sync(queue, ^{
        
        NSLog(@"gujinyue %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"肯定阻塞了");
        });
        //        dispatch_async(queue, ^{
        //            NSLog(@"bu阻塞了 %@",[NSThread currentThread]);
        //        });
    });
}

-(void)Test2
{
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        
        NSURL *url=[NSURL URLWithString:@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg"];
        UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        
        NSLog(@"走在哪里?%@",[NSThread currentThread]);
        //回到主线程显示图片
        dispatch_async(dispatch_get_main_queue(),^{
            
            NSLog(@"回到主线程接受图片 %@",[NSThread currentThread]);
            self.imageView.image=image;
         });
    });
}


//这个方法是按顺序下载两张图片的,但其实下载过程并不需要按顺序执行,并发执行可以提高执行速度。
//但是注意一点就是:必须等两张图片下载完毕后才能回到主线程显示图片,所以可以考虑Dispatch Group
-(void)Test3
{
    //异步下载图片
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        
        //下载第一张图片
        NSString *url1=@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg";
        UIImage *image1=[self imageWithURLString:url1];
        
        NSString *url2=@"http://www.nbdpc.gov.cn/picture/0/s20160225090242.jpg";
        UIImage *image2=[self imageWithURLString:url2];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image=image1;
            self.imageView2.image=image2;
        });
    });
}

//根据url获取UIImage
-(UIImage *)imageWithURLString:(NSString *)urlString
{
    NSURL *url=[NSURL URLWithString:urlString];
    NSData *data=[NSData dataWithContentsOfURL:url];
    
    return [UIImage imageWithData:data];
}

//***************************************************************************************
-(void)Test4
{
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //异步下载图片
    dispatch_async(queue, ^{
        
        //创建一个组
        dispatch_group_t group=dispatch_group_create();
        
        __block UIImage *image1=nil;
        __block UIImage *image2=nil;
        
        //1.关联一个任务到group
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            //下载第一张图片
            NSString *url1=@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg";
            image1=[self imageWithUrlString2:url1];
            
        });
        
        //2.关联一个任务到group
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            //下载第二张图片
            NSString *url2=@"http://www.nbdpc.gov.cn/picture/0/s20160225090242.jpg";
            image2=[self imageWithUrlString2:url2];
        });
        
        //3.等待组中的任务执行完毕,回到主线程执行block回调
        //dispatch_group_notify函数涌来指定一个额外的Block,该block将在group中的所有任务完成后执行
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            self.imageView.image=image1;
            self.imageView2.image=image2;
        });
        
    });
}
-(UIImage *)imageWithUrlString2:(NSString *)urlString
{
    NSURL *url=[NSURL URLWithString:urlString];
    
    NSData *data=[NSData dataWithContentsOfURL:url];
    
    //这里并没有自动释放UIImage对象
    return [[UIImage alloc]initWithData:data];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值