IOS 并发处理
发布日期:2021-11-21 04:40:54 浏览次数:31 分类:技术文章

本文共 3038 字,大约阅读时间需要 10 分钟。

1.并发指的是在同一时间两个或两个以上的任务同时执行并且抢占统一资源的过程

   如果只有一个cpu或许并发就不存在了,但是科技的进步带了并发处理这个问题,因为现在手机双核、四核乃至八核(三星、魅族)

   首先介绍个概念:GCD(Grand Central Dispatch)我的理解就是在同一时间重要资源的派遣处理,是底层的C的用于处理块儿的接口,主要用预处理多任务其中的一条任务是有那个处理器处理。GCD的核心就是一个处理队列,也可以说是一个线程池

1)主线程队列:所有的任务都都是在主队列完成的,大部分是处理UI的东西,可以用dispatch_get_main_queue方法返回到主队列里面,一个application只有一个主线程队列

2)并发队列:dispatch_get_global_queue获得处理并发队列的handle

3) 串性队列:一FIFO模式进行处理,dispatch_queue_create创建该队列,dispatch_release释放该队列

任务分配给不同的队列有两种方法,第一:通过块,第二:通过C方法

2.处理和UI相关的任务:用dispatch_get_main_queue得到主线程

dispatch_async:执行块儿

dispatch_async_f :处理C程序

dispatch_queue_t mainQueue = dispatch_get_main_queue();        dispatch_async(mainQueue, ^{        [[[UIAlertView alloc]initWithTitle:@"GCD" message:@"Amazing" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil] show];    });

3.同步任务下载一个图片,因为每个线程可能在抢占资源,所以有时图片不能显示,代码如下

-(void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(concurrentQueue, ^{                __block UIImage *img = nil;                dispatch_async(concurrentQueue, ^{                        NSString *urlAsString = @"http://f.hiphotos.baidu.com/image/w%3D2048/sign=f57cca190bf79052ef1f403e38cbd6ca/c75c10385343fbf267c91f29b27eca8064388fd7.jpg";                                    NSURL *url = [NSURL URLWithString:urlAsString];                        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];                        NSError *downloadError = nil;                                    NSData *imgData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];                        if(downloadError==nil&&imgData!=nil)            {                            img = [UIImage imageWithData:imgData];                                NSLog(@"data is loading...");                        }            else if(downloadError!=nil)            {                NSLog(@"%@",downloadError);                                    }else            {                NSLog(@"No data could get downloaded form the url");            }                    });                dispatch_async(dispatch_get_main_queue()                       , ^{                                                     if(img!=nil)                           {                               UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];                               [imgView setImage:img];                                                              [imgView setContentMode:UIViewContentModeScaleAspectFill];                                                              [self.view addSubview:imgView];                           }                                                      else                           {                               NSLog(@"Image is not download.Nothing to dispaly");                                                      }                    }); });

(明天再写~~ 今天就学到这里!!!学习真的是件愉快的实情~~~~~)

转载地址:https://blog.csdn.net/xiaoji94shen/article/details/19495797 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:IOS字体设置和颜色设置
下一篇:IOS UIWebView学习

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月07日 02时38分31秒