iOS多线程编程及简单封装图片下载
来源:互联网 发布:制造业数据分析 编辑:程序博客网 时间:2024/05/26 05:53
一、多线程概述
1. 程序:由源代码生成的可执行应用。(wechat就是一个程序)
2. 进程:一个正在运行的程序可以看做一个进程。(正在运行的wechat就是一个进程)
3. 线程:程序中独立运行的代码段。(接收wechat消息的代码)
一个进程由一个或多个线程组成。进程只负责调度与分配,线程才是真正的执行单元,负责代码的执行。
4. 单线程:
4.1 每个正在运行的进程,至少包括一个线程,这个线程就是主线程。
4.2 主线程在程序启动时被创建,用于执行main函数。
4.3 只有一个主线程的程序被称为单线程程序。
4.4 主线程负责执行程序的所有代码,这些代码只能顺序执行,无法并发执行。
4.5 UI的展现只能由主线程修改。
5. 多线程:
5.1:拥有多个线程的程序,称为多线程程序。
5.2:iOS允许开辟新的进程, 但是相对于主线程而言,这些线程被称为子线程。
5.3:子线程与主线程都是独立运行的单元,各自的执行互不影响,因此能够并发执行。
6. 单线程与多线程区别:
单线程程序只有一个线程,代码顺序执行,容易出现阻塞。(页面卡住不动)
多线程程序有多个线程,线程间独立运行,避免了阻塞,也提高了程序的运行性能。
eg.模拟主线程阻塞:
创建了一个按钮, 点击触发一个方法,方法计算1加到635500000。
// 创建一个按钮- (void)createButton{ UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; aButton.frame =CGRectMake(100, 100, 150, 50); [aButton setTitle:@"click" forState:UIControlStateNormal]; [aButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [aButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [aButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:aButton];}// 触发事件- (void)buttonAction:(id)sender{ NSInteger sum = 0; for (int i = 1; i <= 635500000; i++) { sum += i; NSLog(@"%ld", sum); }}
二、iOS多线程实现
1. NSThread
NSTread是一个轻量级的多线程,它有两种创建方法。
注意:在多线程方法中需要添加自动释放池;在程序开始系统会为主线程创建自动释放池;手动创建的子线程需要手动添加释放池。
NSThread主要由两种初始化方法:
// 实例化方法 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculator) object:nil]; [thread start]; [thread release]; // 类方法 [NSThread detachNewThreadSelector:@selector(calculator) toTarget:self withObject:nil];
2. NSOperation & NSOperationQueue
2.1 NSOperation类在MVC中属于model类,是用来封装单个任务相关的代码和数据的抽象类。由于是抽象类,我们不能够直接使用这个类,而是使用子类来执行任务。通常与NSOperationQueue结合使用。
eg.NSOperation与NSOperationQueue结合使用
注意:当最大并发数设置为1的时候,能实现线程同步。
<span style="white-space:pre"></span>CustomOperation *operation = [[CustomOperation alloc] init];// 将opration放入queue中时不能start, queue会自动调用它的方法开始; CustomOperation *operation1 = [[CustomOperation alloc] init]; NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
//<span style="white-space:pre"></span>maxConcurrentOperationCount设置最大并发数量 opQueue.maxConcurrentOperationCount = 1; [opQueue addOperation:operation]; [opQueue addOperation:operation1]; [operation release]; [operation1 release]; [opQueue release];
NSInvocationOperation是NSOperation的子类。封装了执行操作的target和action。
eg.
NSInvocationOperation *invoOpr = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(calculator) object:nil]; NSOperationQueue *queue1 = [[NSOperationQueue alloc] init]; [queue1 addOperation:invoOpr]; [invoOpr release]; [queue1 release];
NSBlockOperation是NSOperation的子类。封装了需要执行操作的代码块。
eg.封装了一个输出0~99的代码块
NSBlockOperation *blockOpr = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool { // block内容就是多线程所要执行的代码; for (int i = 0; i < 100; i++) { NSLog(@"%d", i); } }]; } NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:blockOpr]; [queue release];
3. NSObject实现异步后台执行
NSObject中存在一个最简单的后台执行方法。
eg.
[self performSelectorInBackground:@selector(calculator) withObject:self];
4. GCD(Grand Central Dispatch)
4.1 简介:GCD是苹果公司开发的技术。以优化应用程序支持多核心处理器和其他的对称多处理系统的系统。GCD输入函数级的多线程,性能更高。iOS4以上可用。
GCD以队列的方式工作,先进先出。
GCD中有两种队列:SerialQueue,Concurrent。其中serial一次只执行一个任务。Concurrent可以并发执行多个任务。
4.2 Serial
e.g Serial实例
e.g Concurrent实例
SDWebImage的简单实现
0 0
- iOS多线程编程及简单封装图片下载
- iOS 图片下载封装 ImageDownloader
- iOS 多线程编程<十三、NSOperation图片下载,SDWebImage底层实现原理>
- iOS 多线程编程<十四、SDWebImage图片下载,内存警告处理>
- 图片下载封装
- Android多线程方式处理图片下载及显示
- 多线程图片下载
- 多线程图片下载
- 【ios 网络篇 3】 图片下载及缓存
- iOS个人封装图片下载:Block与Delegate两种方式
- IOS Xib 简单多线程编程代码
- iOS 多线程编程<十五、RunLoop简单介绍>
- IOS 图片下载
- iOS 图片下载
- IOS基础知识之数据库及多线程编程
- iOS多线程编程的定义及方法
- #iOS多线程编程及GCD简介
- 链式编程,iOS简单封装属性字符串,实现富文本.
- freemarker入门例子
- java拼接html(innerHTML)导致未结束的字符串常量
- 使用dataexcel制作复杂报表
- 自选题目安卓APP"任务!任务!"
- hdu4771
- iOS多线程编程及简单封装图片下载
- MySql配置ODBC数据源详解
- NYOJ 39 水仙花数
- iOS中的内存管理机制
- 个人觉得很适合做面试题的一个东东
- uglifier是什么
- 自选题目安卓APP"任务!任务!"
- LUNIX常用命令汇总
- 漫谈加班文化