iOS第三方插件&库iOS开发学习iOS学习笔记

iOS多线程使用总结(NSThread,GCD, NSOpera

2017-02-23  本文已影响0人  香橙柚子

最后更新:2020-08-26

前文

本文主要用于学习记录,概念问题简单说一下,主要写一些使用案例。

iOS 常用的多线程方案有3种.

其中用的最多的就是GCD了,其实还有一种Pthreads,但是实在不常用,所以不太了解,就不说了。

文章中主要使用Objective-C语言,Swift后续会列出来。

1. NSThread

NSThread面向对象,比较直观,但是需要手动管理生命周期,虽然不常用,但是有些方法还是比较好用的,比如[NSThread currentThread]可以获取当前线程,用来调试给常好用。后面我们会经常用到。

   //套路:不要先直接alloc init 先看一下头文件 类方法 如果没有合适的类方法 才 init
   // 1. 创建线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task) object:nil];
    
    //开启线程
    [thread start];
// 新线程调用方法,里边为需要执行的任务
- (void) task {
     NSLog(@"%@", [NSThread currentThread]);
}
//类方法 -- 简单 不能拿到对象  没有机会进行更加详细的设置
[NSThread detachNewThreadSelector:@selector(task) toTarget:self withObject:nil];
- (void) task {
     NSLog(@"%@", [NSThread currentThread]);
}
//隐式创建
[self performSelectorInBackground:@selector(task) withObject:nil];
//取消线程
- (void)cancel;

//启动线程
- (void)start;

//判断某个线程的状态的属性
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled;

//设置和获取线程名字
-(void)setName:(NSString *)n;
-(NSString *)name;

//获取当前线程信息
+ (NSThread *)currentThread;
//获取主线程信息
+ (NSThread *)mainThread;
// 判断是否为主线程(对象方法)
- (BOOL)isMainThread;
// 判断是否为主线程(类方法)
+ (BOOL)isMainThread;    

//使当前线程暂停一段时间,或者暂停到某个时刻
+ (void)sleepForTimeInterval:(NSTimeInterval)time;
+ (void)sleepUntilDate:(NSDate *)date;
// 在主线程上执行操作
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray<NSString *> *)array;
  // equivalent to the first method with kCFRunLoopCommonModes

// 在指定线程上执行操作
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array NS_AVAILABLE(10_5, 2_0);
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);

// 在当前线程上执行操作,调用 NSObject 的 performSelector:相关方法
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

示例DEMO:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建线程下载图片    
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(getIMG) object:nil];
    [thread start];
}
- (void)getIMG{
    [NSThread sleepForTimeInterval:5]; //模拟耗时任务
    
    NSString *strAddress =  @"http://pic41.nipic.com/20140501/2531170_162158900000_2.jpg";
    NSURL *url = [NSURL URLWithString:strAddress];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];

   // 回到主线程刷新界面    
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:NO];
    NSLog(@"over");
}

//回到主线程刷新UI
-(void)updateUI:(UIImage *)image{
    _imageView.image = image;
    NSLog(@"current thread -- %@", [NSThread currentThread]);
}

2. GCD

  1. GCD简介:略
  2. GCD好处:
    GCD 可用于多核的并行运算,最重要的是它会自动管理线程的生命周期(创建线程、调度任务、销毁线程),非常方便.
  3. GCD底层使用的是C语言.但是经过苹果封装非常好用,老少咸宜.

2.1任务和队列

2.2队列的创建以及获取

队列的创建:

// 串行队列的创建方法
dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_SERIAL);
// 并发队列的创建方法
dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_CONCURRENT);

主队列是一个比较特殊的串行队列:

// 主队列获取方法
dispatch_queue_t queue = dispatch_get_main_queue();

GCD 还提供了一个全局并发队列:

// 全局并发队列的获取方法
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

2.3任务的创建

同步执行任务的创建方法: dispatch_sync
异步执行任务创建方法 :dispatch_async。

// 同步执行任务创建方法
dispatch_sync(queue, ^{
    // 这里放同步执行任务代码
});
// 异步执行任务创建方法
dispatch_async(queue, ^{
    // 这里放异步执行任务代码
});

2.4任务和队列不同的组合方式

GCD提供了两种任务执行方式(同步执行和异步执行),提供了两种队列:(串行队列和并行队列),在加上主队列,故而就有多种不同的组合方式。

1.同步执行 + 串行队列
2.同步执行 + 并发队列
3.异步执行 + 串行队列
4.异步执行 + 并发队列
5.同步执行 + 主队列
6.异步执行 + 主队列

区别:

区别 并发队列 串行队列 主队列
同步(sync) 没有开启新线程,串行队列 没有开启新线程,串行执行任务 死锁,不执行
异步(async) 开启新线程,并发执行任务 开启新线程(1条),串行执行任务 没有开启新线程,串行执行任务

2.5 GCD的基本使用(6种组合)

每种情况,我们简单介绍一下,直接代码+打印结果+结论,展示出来
这里的代码以及结论参考了行走少年郎的一篇多线程文章,写的很好,比我的好,所以就把我原来的替换了。大家可以去看他写的。

2.5.1 同步执行 + 串行队列

//打印当前线程
    NSLog(@"当前线程 --- %@",[NSThread currentThread]);
    NSLog(@"同步-串行  ---  begin");
    dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_SERIAL);
    
    //执行任务
    dispatch_sync(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"同步-串行  ---  end");
    

打印结果:

2020-08-18 15:09:05.019655+0800 KX_GCD_Demo[27130:517117] 当前线程 --- <NSThread: 0x600002134f00>{number = 1, name = main}
2020-08-18 15:09:05.019782+0800 KX_GCD_Demo[27130:517117] 同步-串行  ---  begin
2020-08-18 15:09:07.021050+0800 KX_GCD_Demo[27130:517117] 1 --- <NSThread: 0x600002134f00>{number = 1, name = main}
2020-08-18 15:09:09.021304+0800 KX_GCD_Demo[27130:517117] 2 --- <NSThread: 0x600002134f00>{number = 1, name = main}
2020-08-18 15:09:11.021541+0800 KX_GCD_Demo[27130:517117] 3 --- <NSThread: 0x600002134f00>{number = 1, name = main}
2020-08-18 15:09:13.022790+0800 KX_GCD_Demo[27130:517117] 4 --- <NSThread: 0x600002134f00>{number = 1, name = main}
2020-08-18 15:09:13.022947+0800 KX_GCD_Demo[27130:517117] 同步-串行  ---  end

结论:

  1. 所有任务都在当前线程即主线程中执行,没有开启新的线程(同步执行不具备开启线程的能力)
  2. 所有的任务都在beginend之间执行,说明同步执行任务需要等待队列中的任务执行结束
  3. 任务是按照1,2,3,4顺序来执行的,说明串行队列每次只能执行一个任务,需要一个接着一个按顺序执行

2.5.2 同步执行 + 并发队列

  NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    
    NSLog(@"同步-并发 --- begin");
    dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_CONCURRENT);
    
    //执行任务
    dispatch_sync(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"同步-并发 --- end");
   
打印结果:
2020-08-18 15:31:20.826512+0800 KX_GCD_Demo[27676:531403] 当前线程 --- <NSThread: 0x600002db0d40>{number = 1, name = main}
2020-08-18 15:31:20.826660+0800 KX_GCD_Demo[27676:531403] 同步-并发 --- begin
2020-08-18 15:31:22.828083+0800 KX_GCD_Demo[27676:531403] 1 --- <NSThread: 0x600002db0d40>{number = 1, name = main}
2020-08-18 15:31:24.829041+0800 KX_GCD_Demo[27676:531403] 2 --- <NSThread: 0x600002db0d40>{number = 1, name = main}
2020-08-18 15:31:26.830545+0800 KX_GCD_Demo[27676:531403] 3 --- <NSThread: 0x600002db0d40>{number = 1, name = main}
2020-08-18 15:31:28.832078+0800 KX_GCD_Demo[27676:531403] 4 --- <NSThread: 0x600002db0d40>{number = 1, name = main}
2020-08-18 15:31:28.832398+0800 KX_GCD_Demo[27676:531403] 同步-并发 --- end

结论:

  1. 所有任务都是在当前线程即主线程中执行的,没有开启新的线程(同步执行不具备开启新线程的能力)
  2. 所有任务都在begin和end之间执行的(同步执行需要等待队列中,前边的任务执行完毕,再继续执行)
  3. 任务是按顺序执行.这里特殊说明一下:虽然并发队列可以开启多个线程,同时执行多个任务.但是因为本身不具备开线程的能力,只有当前这一个线程(同步执行不具备开线程能力).
    所以不能并发执行,只能顺序执行.而且当前线程只有等待当前队列中正在执行的任务执行完毕之后,才能继续执行.
    所以任务只能一个接着一个顺序执行,不能同时被执行.

2.5.3 异步执行 + 串行队列

NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    
    NSLog(@"异步-串行 --- begin");
    dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_SERIAL);
    
    //执行任务
    dispatch_async(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"异步-串行 --- end");

打印结果:

2020-08-18 15:58:20.620164+0800 KX_GCD_Demo[27846:545723] 当前线程 --- <NSThread: 0x60000269c240>{number = 1, name = main}
2020-08-18 15:58:20.620322+0800 KX_GCD_Demo[27846:545723] 异步-串行 --- begin
2020-08-18 15:58:20.620464+0800 KX_GCD_Demo[27846:545723] 异步-串行 --- end
2020-08-18 15:58:22.621062+0800 KX_GCD_Demo[27846:545800] 1 --- <NSThread: 0x6000026c4180>{number = 7, name = (null)}
2020-08-18 15:58:24.625413+0800 KX_GCD_Demo[27846:545800] 2 --- <NSThread: 0x6000026c4180>{number = 7, name = (null)}
2020-08-18 15:58:26.626524+0800 KX_GCD_Demo[27846:545800] 3 --- <NSThread: 0x6000026c4180>{number = 7, name = (null)}
2020-08-18 15:58:28.629949+0800 KX_GCD_Demo[27846:545800] 4 --- <NSThread: 0x6000026c4180>{number = 7, name = (null)}

结论:

  1. 开启了一条新的线程(异步执行具备开线程的能力,串行队列只能开启一个线程)
  2. 所有任务都是在begin和end之后才开始执行(异步执行不会做任何等待,可以开线程继续执行任务)
  3. 任务是顺序执行的(串行队列每次只能执行一个任务,任务是按顺序执行的)

2.5.4 异步执行 + 并发队列

 NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    
    NSLog(@"异步-并发 --- begin");
    dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_CONCURRENT);
    
    //执行任务
    dispatch_async(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"异步-并发 --- end");

打印结果:

2020-08-18 16:04:42.114241+0800 KX_GCD_Demo[28014:554532] 当前线程 --- <NSThread: 0x6000024d0ec0>{number = 1, name = main}
2020-08-18 16:04:42.114406+0800 KX_GCD_Demo[28014:554532] 异步-并发 --- begin
2020-08-18 16:04:42.114557+0800 KX_GCD_Demo[28014:554532] 异步-并发 --- end
2020-08-18 16:04:44.116285+0800 KX_GCD_Demo[28014:554707] 4 --- <NSThread: 0x600002480500>{number = 4, name = (null)}
2020-08-18 16:04:44.116285+0800 KX_GCD_Demo[28014:554706] 2 --- <NSThread: 0x600002495800>{number = 5, name = (null)}
2020-08-18 16:04:44.116285+0800 KX_GCD_Demo[28014:554705] 1 --- <NSThread: 0x600002487880>{number = 8, name = (null)}
2020-08-18 16:04:44.116360+0800 KX_GCD_Demo[28014:554708] 3 --- <NSThread: 0x60000249cd40>{number = 6, name = (null)}

结论:

  1. 除了当前线程(主线程),系统又开启4个新的线程,并且任务是交替执行的(同时执行)。说明异步执行具备开线程的能力,并且并发队列可以开启多个线程,同时执行多个任务。
  2. 所有任务都是在begin和end之后执行的,说明当前线程没有等待,而是直接开启了新线程,在新线程中执行任务的。

2.5.5 同步执行 + 主队列(在主线程调用)

今天只演示在主线程调用:

- (void)text5{

    NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    
    NSLog(@"同步执行-主队列 --- begin");
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    //执行任务
    dispatch_sync(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_sync(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"同步执行-主队列 --- end");
 }

打印结果:

2020-08-18 16:27:17.592790+0800 KX_GCD_Demo[28140:565766] 当前线程 --- <NSThread: 0x600003954d40>{number = 1, name = main}
2020-08-18 16:27:17.592934+0800 KX_GCD_Demo[28140:565766] 同步执行-主队列 --- begin
(lldb)

结论:
同步执行 + 主队列
结论:

面试经常会问这个问题。

2.5.6 异步执行 + 主队列

NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    
    NSLog(@"异步执行-主队列 --- begin");
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    //执行任务
    dispatch_async(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });
    NSLog(@"异步执行-主队列 --- end");

打印结果:

2020-08-18 20:40:28.667081+0800 KX_GCD_Demo[41989:696384] 当前线程 --- <NSThread: 0x6000015ccac0>{number = 1, name = main}
2020-08-18 20:40:28.667221+0800 KX_GCD_Demo[41989:696384] 异步执行-主队列 --- begin
2020-08-18 20:40:28.667368+0800 KX_GCD_Demo[41989:696384] 异步执行-主队列 --- end
2020-08-18 20:40:30.682547+0800 KX_GCD_Demo[41989:696384] 1 --- <NSThread: 0x6000015ccac0>{number = 1, name = main}
2020-08-18 20:40:32.683854+0800 KX_GCD_Demo[41989:696384] 2 --- <NSThread: 0x6000015ccac0>{number = 1, name = main}
2020-08-18 20:40:34.684356+0800 KX_GCD_Demo[41989:696384] 3 --- <NSThread: 0x6000015ccac0>{number = 1, name = main}
2020-08-18 20:40:36.685728+0800 KX_GCD_Demo[41989:696384] 4 --- <NSThread: 0x6000015ccac0>{number = 1, name = main}

结论:

  1. 所有任务都是在当前线程执行的,并没有开启新的线程(虽然异步执行具备开启线程的能力,但是你因为是主队列,所以所有任务都在主线程中)
  2. 所有任务是在begin和end之后才执行的.(异步不等待,可以继续执行)
  3. 任务是按顺序执行的,主队列其实也是一个串行的队列

2.6 线程间的通信

线程通信案例:子线程处理完耗时操作后,回到主线程刷新UI,这样就完成了线程之间的通信。
案例:

 //获取全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //获取主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        //异步追加任务1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
        //回到主线程
        dispatch_async(mainQueue, ^{
            [NSThread sleepForTimeInterval:2];  //模拟耗时操作
            NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
        });
    });

2.7GCD中的其他方法

2.7.1栅栏方法

使用情况:第一组(多个)任务执行完成之后,再进行第二组操作。相当于在两组之间添加了一个栅栏,阻隔开两组任务的执行。这里就用到了dispatch_barrier_async方法。

 
    //栅栏方法
    dispatch_queue_t queue = dispatch_queue_create("gcd.name", DISPATCH_QUEUE_CONCURRENT);
    
    //执行任务
    dispatch_async(queue, ^{
        //追加任务 1
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"1 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 2
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"2 --- %@",[NSThread currentThread]); //打印当前线程
    });
    
    //栅栏方法
    dispatch_barrier_async(queue, ^{
        //追加任务barrier
        [NSThread sleepForTimeInterval:2];//模拟耗时操作
        NSLog(@"barrier --- %@",[NSThread currentThread]); //打印当前线程
    });
    
    
    dispatch_async(queue, ^{
        //追加任务 3
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"3 --- %@",[NSThread currentThread]); //打印当前线程
    });
    dispatch_async(queue, ^{
        //追加任务 4
        [NSThread sleepForTimeInterval:2];  //模拟耗时操作
        NSLog(@"4 --- %@",[NSThread currentThread]); //打印当前线程
    });

打印结果:

2020-08-19 11:40:11.637863+0800 KX_GCD_Demo[44619:935467] 2 --- <NSThread: 0x600001cc6d40>{number = 5, name = (null)}
     2020-08-19 11:40:11.637871+0800 KX_GCD_Demo[44619:935470] 1 --- <NSThread: 0x600001cc6bc0>{number = 3, name = (null)}
     2020-08-19 11:40:13.638746+0800 KX_GCD_Demo[44619:935467] barrier --- <NSThread: 0x600001cc6d40>{number = 5, name = (null)}
     2020-08-19 11:40:15.640520+0800 KX_GCD_Demo[44619:935470] 4 --- <NSThread: 0x600001cc6bc0>{number = 3, name = (null)}
     2020-08-19 11:40:15.640521+0800 KX_GCD_Demo[44619:935467] 3 --- <NSThread: 0x600001cc6d40>{number = 5, name = (null)}

结论:在执行完栅栏前面的操作之后,才执行栅栏操作,最后再执行栅栏后边的操作。

2.7.2 延时执行 dispatch_after

在项目中延时执行有多种方法,dispatch_after就是其中一个。

    NSLog(@"当前线程 --- %@",[NSThread currentThread]);//打印当前线程
    NSLog(@"after --- begin");

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,  (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"after --- %@",[NSThread currentThread]);//打印当前线程
    });

打印结果:

2020-08-19 15:24:52.379680+0800 KX_GCD_Demo[45859:1033251] 当前线程 --- <NSThread: 0x600002e1c0c0>{number = 1, name = main}
2020-08-19 15:24:52.379818+0800 KX_GCD_Demo[45859:1033251] after --- begin
2020-08-19 15:24:54.380257+0800 KX_GCD_Demo[45859:1033251] after --- <NSThread: 0x600002e1c0c0>{number = 1, name = main}

2.7.3 单通道执行(只执行一次):dispatch_once

常用的就是单例的情况了:

static id _instance;
+(instancetype)shared{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

2.7.4 队列组:dispatch_group

案例情况:执行多个任务,执行完成之后再回到主线程去执行任务。
有多种队列组的方法可以实现。

  1. dispatch_group_notify
    监听 group 中任务的完成状态,当所有的任务都执行完成后,追加任务到 group 中,并执行任务。
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"group---begin");
    
    dispatch_group_t group =  dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务 1
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务 2
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // 等前面的异步任务 1、任务 2 都执行完毕后,回到主线程执行下边任务
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"3---%@",[NSThread currentThread]);      // 打印当前线程
    });
        NSLog(@"group---end");

打印结果:

2020-08-25 22:24:40.382897+0800 KX_GCD_Demo[1429:33018] currentThread---<NSThread: 0x600001638ec0>{number = 1, name = main}
2020-08-25 22:24:40.383599+0800 KX_GCD_Demo[1429:33018] group---begin
2020-08-25 22:24:40.384450+0800 KX_GCD_Demo[1429:33018] group---end
2020-08-25 22:24:42.387804+0800 KX_GCD_Demo[1429:33234] 2---<NSThread: 0x600001678080>{number = 5, name = (null)}
2020-08-25 22:24:42.387766+0800 KX_GCD_Demo[1429:33237] 1---<NSThread: 0x600001672700>{number = 6, name = (null)}
2020-08-25 22:24:44.389329+0800 KX_GCD_Demo[1429:33018] 3---<NSThread: 0x600001638ec0>{number = 1, name = main}

  1. dispatch_group_wait
    阻塞当前线程,等待指定的 group 中所有的任务执行完成后,才会继续往下执行。
 NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"group---begin");
    
    dispatch_group_t group =  dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务 1
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务 2
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
    });
    // 等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程)
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"group---end");

打印结果:

2020-08-25 22:28:13.240155+0800 KX_GCD_Demo[1461:35597] currentThread---<NSThread: 0x600001ba8380>{number = 1, name = main}
2020-08-25 22:28:13.240345+0800 KX_GCD_Demo[1461:35597] group---begin
2020-08-25 22:28:15.242472+0800 KX_GCD_Demo[1461:35685] 1---<NSThread: 0x600001becc00>{number = 5, name = (null)}
2020-08-25 22:28:15.242493+0800 KX_GCD_Demo[1461:35687] 2---<NSThread: 0x600001bfcd80>{number = 7, name = (null)}
2020-08-25 22:28:15.242974+0800 KX_GCD_Demo[1461:35597] group---end

注意看打印结果的时间,是阻塞线程之后再执行的end.

  1. dispatch_group_enter、dispatch_group_leave
    这是我最喜欢用的实现方案。
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程

    NSLog(@"group---begin");
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        // 追加任务 1
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        // 追加任务 2
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // 等前面的异步操作都执行完毕后,回到主线程.
        [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
        NSLog(@"3---%@",[NSThread currentThread]);      // 打印当前线程
    });
        NSLog(@"group---end");

打印结果:

2020-08-25 22:33:53.365593+0800 KX_GCD_Demo[1511:38898] currentThread---<NSThread: 0x600003128900>{number = 1, name = main}
2020-08-25 22:33:53.365755+0800 KX_GCD_Demo[1511:38898] group---begin
2020-08-25 22:33:53.365962+0800 KX_GCD_Demo[1511:38898] group---end
2020-08-25 22:33:55.368889+0800 KX_GCD_Demo[1511:38963] 2---<NSThread: 0x600003169ec0>{number = 4, name = (null)}
2020-08-25 22:33:55.368890+0800 KX_GCD_Demo[1511:38966] 1---<NSThread: 0x6000031632c0>{number = 7, name = (null)}
2020-08-25 22:33:57.370401+0800 KX_GCD_Demo[1511:38898] 3---<NSThread: 0x600003128900>{number = 1, name = main}

  1. 信号量:dispatch_semaphore
    Dispatch Semaphore 中,使用计数来完成这个功能,计数小于 0 时等待,不可通过。计数为 0 或大于 0 时,计数减 1 且不等待,可通过。
    Dispatch Semaphore提供了三个方法:

3. NSOperation和NSOperationQueue

NSOperation、NSOperationQueue是完全级别的封装,完全面向对象。比 GCD 更简单易用、代码可读性也更高。

执行步骤:

  1. 将要执行的任务封装到一个 NSOperation 对象中。
  2. 将此任务添加到一个 NSOperationQueue 对象中。

值得说明的是,NSOperation 只是一个抽象类,所以不能封装任务。但它有 2 个子类用于封装任务。分别是:NSInvocationOperation 和 NSBlockOperation 。创建一个 Operation 后,需要调用 start 方法来启动任务,它会默认在当前队列同步执行。当然你也可以在中途取消一个任务,只需要调用其 cancel 方法即可。

3.1 子类NSInvocationOperation

- (void)demo  {
   // NSOperation *op = [[NSOperation alloc]init];
    //NSOperation 是抽象类(有定义没有实现),你不能直接使用,需要用子类,或者系统已经给你提供好了2个之类 NSInvocationOperation NSBlockOperation
    
    //1.创建NSInvocationOperation对象
  NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task) object:nil];

  //2.开始执行
  [operation start];
}

- (void)task{
    NSLog(@"task %@",[NSThread currentThread]);
}

在没有使用NSOperationQueue的情况下,NSInvocationOperation是在当前线程执行的操作。

3.2 子类NSBlockOperation

- (void)useBlockOperation {

    // 1.创建 NSBlockOperation 对象
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];

    // 2.开始执行操作
    [op start];
}

操作是在当前线程执行的,并没有开启新线程。

NSBlockOperation还提供了addExecutionBlock方法,追加可执行的block。
例如:


    NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"task1  %@",[NSThread currentThread]);
    }];
    
    //追加可执行的block
    [op1 addExecutionBlock:^{
       
        NSLog(@"task2  %@",[NSThread currentThread]);
 
    }];
[op1 start];

3.3创建队列

  1. 自定义队列
    • 添加到这种队列中的操作,就会自动放到子线程中执行。
    • 同时包含了:串行、并发功能。
// 主队列获取方法
NSOperationQueue *queue = [NSOperationQueue mainQueue];
  1. 主队列
    -凡是添加到主队列中的操作,都会放到主线程中执行(特殊情况除外)
// 自定义队列创建方法
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

3.4 将操作添加到队列中

3.4.1 使用addOperation

简单的例子:

 NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task) object:nil];
    
    //2.队列 并发
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //把操作添加到队列中
    [queue addOperation:op1];

还比如:

-(void)demo2
{
    NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"task1  %@",[NSThread currentThread]);
    }];
    
    //追加可执行的block
    [op1 addExecutionBlock:^{
       
        NSLog(@"task2  %@",[NSThread currentThread]);
 
    }];

      queue = [[NSOperationQueue alloc]init];

    //开了新的线程
    [queue addOperation:op1];
}

开启了新的线程。

也可以两种子类结合起来用:

/**
 * 使用 addOperation: 将操作加入到操作队列中
 */
- (void)addOperationToQueue {

    // 1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // 2.创建操作
    // 使用 NSInvocationOperation 创建操作1
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil];

    // 使用 NSInvocationOperation 创建操作2
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task2) object:nil];

    // 使用 NSBlockOperation 创建操作3
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    [op3 addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"4---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];

    // 3.使用 addOperation: 添加所有操作到队列中
    [queue addOperation:op1]; // [op1 start]
    [queue addOperation:op2]; // [op2 start]
    [queue addOperation:op3]; // [op3 start]
    
    NSLog(@"over");
}

-(void)task1{
    [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
    NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
}
- (void)task2{
    [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
    NSLog(@"2---%@", [NSThread currentThread]); // 打印当前线程
}

打印结果:

2020-08-26 21:52:45.953187+0800 05.NSOperation[1059:29510] over
2020-08-26 21:52:47.954189+0800 05.NSOperation[1059:29738] 1---<NSThread: 0x60000155b280>{number = 6, name = (null)}
2020-08-26 21:52:47.954199+0800 05.NSOperation[1059:29736] 3---<NSThread: 0x600001554000>{number = 5, name = (null)}
2020-08-26 21:52:47.954193+0800 05.NSOperation[1059:29739] 4---<NSThread: 0x600001550180>{number = 2, name = (null)}
2020-08-26 21:52:47.954209+0800 05.NSOperation[1059:29737] 2---<NSThread: 0x600001550680>{number = 4, name = (null)}
2020-08-26 21:52:49.958189+0800 05.NSOperation[1059:29739] 4---<NSThread: 0x600001550180>{number = 2, name = (null)}
2020-08-26 21:52:49.958218+0800 05.NSOperation[1059:29736] 3---<NSThread: 0x600001554000>{number = 5, name = (null)}

结论:先打印的over,全部在子线程,并发执行。
使用 NSOperation 子类创建操作,使用 addOperation:将操作加入到操作队列后有开启新线程的能力。

另外:操作队列添加操作还有一个方法,可以一次添加多个操作。

/**
 添加多个操作到队列
 一个操作只能够被添加到一个队列中
 */
- (void)demo3 {
   NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"task1  %@",[NSThread currentThread]);
   }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"task2  %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:0.5];
    }];
    
//    [self.queue addOperation:op1];
//    [self.queue addOperation:op2];

    [self.queue addOperations:@[op1,op2] waitUntilFinished:NO];
    //当NO的时候不阻塞,当yes的时候会阻塞后面的代码
    NSLog(@"over");
}

[self.queue addOperations:@[op1,op2] waitUntilFinished:NO];
当NO的时候不阻塞,当yes的时候会阻塞后面的代码

3.4.2 使用addOperationWithBlock

这种方法不需要事先创建操作,直接将操作添加到block中。

 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

 [queue addOperationWithBlock:^{
        NSLog(@"task--  %@",[NSThread currentThread]);
    }];

打印结果:

2020-08-26 22:03:29.328099+0800 05.NSOperation[1110:35213] task1  <NSThread: 0x600003fe4080>{number = 3, name = (null)}

仅仅添加了一个方法,就已经开线程了,说明使用
addOperationWithBlock将操作加入到操作队列后能够开启新线程。
那我们多使用几次addOperationWithBlock就会发现不但可以开线程,而且是并发执行。

3.5 NSOperationQueue 控制串行执行、并发执行

这也是我任务NSOperationQueue 最厉害的地方,它可以控制开线程的个数。
关键属性最大并发操作数maxConcurrentOperationCount,用来控制队列。
理解为开多少个线程不够全面,但是方便理解。正确解释为:设置队列中同一时间能同时运行多少个类型的任务操作
引自网络博主江苏小白龙的一条评论,接的说的挺有道理,摘抄过来 :认为maxConcurrentOperationCount = 1,NSOperationQueue就成了串行队列不够严谨,当队列中有2种类型的任务操作时而其中一种任务类型追加了2个子类型的任务操作,当maxConcurrentOperationCount = 1时只能同时运行一种任务操作串行运行的,当他运行到有子类型任务操作的时候那么他会开启新的线程同步执行追加的任务。所以本人认为maxConcurrentOperationCount属性的作用是:设置队列中同一时间片能同时运行多少个类型的任务操作。

案例:


/**
 * 设置 MaxConcurrentOperationCount(最大并发操作数)
 */
- (void)setupMaxConcurrentOperationCountDemo {
    
    // 1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.设置最大并发操作数
    queue.maxConcurrentOperationCount = 1; // 串行队列
    // queue.maxConcurrentOperationCount = 2; // 并发队列
    // queue.maxConcurrentOperationCount = 4; // 并发队列
    
    // 3.添加操作
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"2---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"4---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模拟耗时操作
            NSLog(@"5---%@", [NSThread currentThread]); // 打印当前线程
        }
    }];
    
    NSLog(@"end");
}

最大并发操作数为1 时,打印结果:

2020-08-26 22:16:17.101511+0800 05.NSOperation[1158:40941] end
2020-08-26 22:16:19.106345+0800 05.NSOperation[1158:40992] 1---<NSThread: 0x600000a2e280>{number = 4, name = (null)}
2020-08-26 22:16:21.112007+0800 05.NSOperation[1158:40992] 1---<NSThread: 0x600000a2e280>{number = 4, name = (null)}
2020-08-26 22:16:23.116690+0800 05.NSOperation[1158:40992] 2---<NSThread: 0x600000a2e280>{number = 4, name = (null)}
2020-08-26 22:16:25.122301+0800 05.NSOperation[1158:40992] 2---<NSThread: 0x600000a2e280>{number = 4, name = (null)}
2020-08-26 22:16:27.123054+0800 05.NSOperation[1158:41041] 3---<NSThread: 0x600000a39080>{number = 7, name = (null)}
2020-08-26 22:16:29.124319+0800 05.NSOperation[1158:41041] 3---<NSThread: 0x600000a39080>{number = 7, name = (null)}
2020-08-26 22:16:31.129149+0800 05.NSOperation[1158:41041] 4---<NSThread: 0x600000a39080>{number = 7, name = (null)}
2020-08-26 22:16:33.133247+0800 05.NSOperation[1158:41041] 4---<NSThread: 0x600000a39080>{number = 7, name = (null)}
2020-08-26 22:16:35.138359+0800 05.NSOperation[1158:41041] 5---<NSThread: 0x600000a39080>{number = 7, name = (null)}
2020-08-26 22:16:37.143127+0800 05.NSOperation[1158:41041] 5---<NSThread: 0x600000a39080>{number = 7, name = (null)}

最大并发操作数为2 时,打印结果:

2020-08-26 22:19:27.567659+0800 05.NSOperation[1195:42965] end
2020-08-26 22:19:29.571120+0800 05.NSOperation[1195:43066] 1---<NSThread: 0x600002a84c80>{number = 6, name = (null)}
2020-08-26 22:19:29.571145+0800 05.NSOperation[1195:43068] 2---<NSThread: 0x600002a9c0c0>{number = 5, name = (null)}
2020-08-26 22:19:31.576032+0800 05.NSOperation[1195:43068] 2---<NSThread: 0x600002a9c0c0>{number = 5, name = (null)}
2020-08-26 22:19:31.576042+0800 05.NSOperation[1195:43066] 1---<NSThread: 0x600002a84c80>{number = 6, name = (null)}
2020-08-26 22:19:33.577990+0800 05.NSOperation[1195:43066] 4---<NSThread: 0x600002a84c80>{number = 6, name = (null)}
2020-08-26 22:19:33.578004+0800 05.NSOperation[1195:43070] 3---<NSThread: 0x600002a9b380>{number = 3, name = (null)}
2020-08-26 22:19:35.582109+0800 05.NSOperation[1195:43070] 3---<NSThread: 0x600002a9b380>{number = 3, name = (null)}
2020-08-26 22:19:35.582133+0800 05.NSOperation[1195:43066] 4---<NSThread: 0x600002a84c80>{number = 6, name = (null)}
2020-08-26 22:19:37.584611+0800 05.NSOperation[1195:43068] 5---<NSThread: 0x600002a9c0c0>{number = 5, name = (null)}
2020-08-26 22:19:39.588468+0800 05.NSOperation[1195:43068] 5---<NSThread: 0x600002a9c0c0>{number = 5, name = (null)}

最大并发操作数为4 时,打印结果:

2020-08-26 22:20:12.711985+0800 05.NSOperation[1213:43733] end
2020-08-26 22:20:14.715188+0800 05.NSOperation[1213:43826] 1---<NSThread: 0x600000bfd900>{number = 4, name = (null)}
2020-08-26 22:20:14.715204+0800 05.NSOperation[1213:43825] 4---<NSThread: 0x600000bb92c0>{number = 8, name = (null)}
2020-08-26 22:20:14.715239+0800 05.NSOperation[1213:43827] 2---<NSThread: 0x600000bfca80>{number = 6, name = (null)}
2020-08-26 22:20:14.715304+0800 05.NSOperation[1213:43828] 3---<NSThread: 0x600000bb9380>{number = 7, name = (null)}
2020-08-26 22:20:16.717543+0800 05.NSOperation[1213:43827] 2---<NSThread: 0x600000bfca80>{number = 6, name = (null)}
2020-08-26 22:20:16.717550+0800 05.NSOperation[1213:43826] 1---<NSThread: 0x600000bfd900>{number = 4, name = (null)}
2020-08-26 22:20:16.717543+0800 05.NSOperation[1213:43825] 4---<NSThread: 0x600000bb92c0>{number = 8, name = (null)}
2020-08-26 22:20:16.717544+0800 05.NSOperation[1213:43828] 3---<NSThread: 0x600000bb9380>{number = 7, name = (null)}
2020-08-26 22:20:18.721737+0800 05.NSOperation[1213:43827] 5---<NSThread: 0x600000bfca80>{number = 6, name = (null)}
2020-08-26 22:20:20.726078+0800 05.NSOperation[1213:43827] 5---<NSThread: 0x600000bfca80>{number = 6, name = (null)}

结论:当最大并发操作数为1时,操作是按顺序串行执行的。当最大操作并发数为2时,操作是并发执行的,可以同时执行两个操作。而开启线程数量是由系统决定的,不需要我们来管理。当最大操作并发数为4时,操作是并发执行的,可以同时执行4个操作。

3.5操作依赖

NSOperation 有一个非常实用的功能,那就是添加依赖。
比如我们有 4 个任务:1. 登录APP,2.才能付款买东西,3.下载我们要的资料,4. 回到主线程刷新UI。这时就可以用到依赖了:
Demo:

- (void)demo {
    
    /**
     1.登录
     2.付款
     3.下载
     4.UI
     
     1,2,3,4 有序执行
     1,2,3 都是耗时任务 --> 子线程
     4 --> 主线程
     串行队列,异步执行
     */
    
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
       
        NSLog(@"登录 ,%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"付款 ,%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"下载 ,%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"UI ,%@",[NSThread currentThread]);
    }];
    
    [op2 addDependency:op1];
    [op3 addDependency:op2];
    [op4 addDependency:op3];
    
    [self.queue addOperations:@[op1,op2,op3] waitUntilFinished:NO];
    
    [[NSOperationQueue mainQueue] addOperation:op4];
    
}

还有一些其他方法:
NSOperation:

BOOL executing; //判断任务是否正在执行
BOOL finished; //判断任务是否完成
void (^completionBlock)(void); //用来设置完成后需要执行的操作
- (void)cancel; //取消任务
- (void)waitUntilFinished; //阻塞当前线程直到此任务执行完毕

NSOperationQueue

NSUInteger operationCount; //获取队列的任务数
- (void)cancelAllOperations; //取消队列中所有的任务
- (void)waitUntilAllOperationsAreFinished; //阻塞当前线程直到此队列中的所有任务执行完毕
[queue setSuspended:YES]; // 暂停queue
[queue setSuspended:NO]; // 继续queue

总结:这大概就是GCD,NSThread,NSOperation的全部用法了,但是还有一些如线程安全的没有写。有空再写吧。
这里面很多demo参考了几个优秀博主的代码。文中有写出来,除此之外还,整体结构还参考了伯恩的遗产的一篇文章。非常感谢。
后续会把文中代码转换为Swift再列出来。

喜欢就点个赞再走吧。

上一篇 下一篇

猜你喜欢

热点阅读