一. 自己总结的线程;进程;并行;并发;任务之间的关系图
![这里写图片描述](https://img-blog.csdn.net/20160314230211632)
二. 使用Crearte函数创建的并发队列和全局并发队列的主要区别:
1)全局并发队列在整个应用程序中本身是默认存在的并且对应有高优先级、默认优先级、低优先级和后台优先级一共四个并发队列,我们只是选择其中的一个直接拿来用。而Create函数是实打实的从头开始去创建一个队列。
2)在iOS6.0之前,在GCD中凡是使用了带Create和retain的函数在最后都需要做一次release操作。而主队列和全局并发队列不需要我们手动release。当然了,在iOS6.0之后GCD已经被纳入到了ARC的内存管理范畴中,即便是使用retain或者create函数创建的对象也不再需要开发人员手动释放,我们像对待普通OC对象一样对待GCD就OK
3)在使用栅栏函数的时候,苹果官方明确规定栅栏函数只有在和使用create函数自己的创建的并发队列一起使用的时候才有效(没有给出具体原因)
4)其它区别涉及到XNU内核的系统级线程编程,不一一列举。
三. 单例模式
1 基本概念
1)单例模式
在程序运行过程,一个类只有一个实例
2)使用场合
在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次)
2 ARC实现单例
1)步骤
01 在类的内部提供一个static修饰的全局变量
02 提供一个类方法,方便外界访问
03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法
2)相关代码
static XMGTools *_instance;
+(instancetype)shareTools
{
return [[self alloc]init];
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
1. mutableCopy 创建一个新的可变对象,并初始化为原对象的值,新对象的引用计数为 1;
2. copy 返回一个不可变对象。分两种情况:(1)若原对象是不可变对象,那么返回原对象,并将其引用计数加 1 ;(2)若原对象是可变对象,那么创建一个新的不可变对象,并初始化为原对象的值,新对象的引用计数为 1。
-(nonnull id)copyWithZone:(nullable NSZone *)zone
{
return _instance;
}
-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone
{
return _instance;
}
3 MRC实现单例
1)实现步骤
01 在类的内部提供一个static修饰的全局变量
02 提供一个类方法,方便外界访问
03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法
05 重写release方法
06 重写retain方法
07 建议在retainCount方法中返回一个最大值
2)配置MRC环境知识
01 注意ARC不是垃圾回收机制,是编译器特性
02 配置MRC环境:build setting ->搜索automatic ref->修改为NO
3)相关代码
static XMGTools *_instance;
+(instancetype)shareTools
{
return [[self alloc]init];
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
-(nonnull id)copyWithZone:(nullable NSZone *)zone
{
return _instance;
}
-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone
{
return _instance;
}
-(oneway void)release
{
}
-(instancetype)retain
{
return _instance;
}
-(NSUInteger)retainCount
{
return MAXFLOAT;
}
4 通用版本
1)有意思的对话
01 问:写一份单例代码在ARC和MRC环境下都适用?
答:可以使用条件编译来判断当前项目环境是ARC还是MRC
02 问:条件编译的代码呢,么么哒?
答:条件编译
#if __has_feature(objc_arc)
//如果是ARC,那么就执行这里的代码1
#else
//如果不是ARC,那么就执行代理的代码2
#endif
03 问:在项目里面往往需要实现很多的单例,比如下载、网络请求、音乐播放等等,弱弱的问一句单例可以用继承吗?
答:单例是不可以用继承的,如果想一次写就,四处使用,那么推荐亲使用带参数的宏定义啦!
2)使用带参数的宏完成通用版单例模式代码
01 注意条件编译的代码不能包含在宏定义里面
02 宏定义的代码只需要写一次就好,之后直接拖到项目中用就OK
四. NSOperation
(1)NSOperation基本使用
1)相关概念
01 NSOperation是对GCD的包装
02 两个核心概念【队列+操作】
2)基本使用
01 NSOperation本身是抽象类,只能只有它的子类
02 三个子类分别是:NSBlockOperation、NSInvocationOperation以及自定义继承自NSOperation的类
03 NSOperation和NSOperationQueue结合使用实现多线程并发
3)相关代码
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self selector:@selector(run) object:nil];
[operation start];
---------------
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"---download1--%@",[NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"---download2--%@",[NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"---download3--%@",[NSThread currentThread]);
}];
[operation start];
---------------
-(void)main
{
NSLog(@"--main--%@",[NSThread currentThread]);
}
XMGOperation *op = [[XMGOperation alloc]init];
[op start];
(2)NSOperationQueue基本使用
1)NSOperation中的两种队列
01 主队列 通过mainQueue获得,凡是放到主队列中的任务都将在主线程执行
02 非主队列 直接alloc init出来的队列。非主队列同时具备了并发和串行的功能,通过设置最大并发数属性来控制任务是并发执行还是串行执行
2)相关代码
-(void)customOperation
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
XMGOperation *op1 = [[XMGOperation alloc]init];
XMGOperation *op2 = [[XMGOperation alloc]init];
[queue addOperation:op1];
[queue addOperation:op2];
}
- (void)block
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1----%@",[NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2----%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
NSLog(@"3----%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
NSLog(@"4----%@",[NSThread currentThread]);
}];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperationWithBlock:^{
NSLog(@"5----%@",[NSThread currentThread]);
}];
}
- (void)invocation
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
}
(3) NSOperation其它用法
1)设置最大并发数【控制任务并发和串行】
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = 2;
2)暂停和恢复以及取消
if (self.queue.isSuspended) {
self.queue.suspended = NO;
}else
{
self.queue.suspended = YES;
}
[self.queue cancelAllOperations];
---------自定义NSOperation取消操作--------------------------
-(void)main
{
for (int i = 0; i<1000; i++) {
NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
}
NSLog(@"+++++++++++++++++++++++++++++++++");
if (self.isCancelled) {
return;
}
for (int i = 0; i<1000; i++) {
NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
}
NSLog(@"+++++++++++++++++++++++++++++++++");
}
(4) NSOperation实现线程间通信
1)开子线程下载图片
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"下载图片操作--%@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"刷新UI操作---%@",[NSThread currentThread]);
}];
}];
2)下载多张图片合成综合案例(设置操作依赖)
//02 综合案例
- (void)download2
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.image1 = [UIImage imageWithData:data];
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/13/87/82/27Q58PICYje_1024.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.image2 = [UIImage imageWithData:data];
}];
NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];
[self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"刷新UI---%@",[NSThread currentThread]);
}];
}];
[combine addDependency:op1];
[combine addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:combine];
}
( 5) CD和NSOperation的对比:
1)GCD是纯C语言的API,而操作队列则是Object-C的对象。
2)在GCD中,任务用块(block)来表示,而块是个轻量级的数据结构;相反操作队列中的『操作』NSOperation则是个更加重量级的Object-C对象。
3)具体该使用GCD还是使用NSOperation需要看具体的情况
五. NSOperation和NSOperationQueue相对GCD的好处有:
1)NSOperationQueue可以方便的调用cancel方法来取消某个操作,而GCD中的任务是无法被取消的(安排好任务之后就不管了)。
2)NSOperation可以方便的指定操作间的依赖关系。
3)NSOperation可以通过KVO提供对NSOperation对象的精细控制(如监听当前操作是否被取消或是否已经完成等)
4)NSOperation可以方便的指定操作优先级。操作优先级表示此操作与队列中其它操作之间的优先关系,优先级高的操作先执行,优先级低的后执行。
5)通过自定义NSOperation的子类可以实现操作重用.