iOS复习----多线程(一)

2021-06-07  本文已影响0人  脚踏实地的小C

一、线程和进程

关系:

       我们可以这么记两者的关系:进程相当于公司中的部门,线程是部门里的员工

相同点:
       都是操作系统所提供的程序执行的基本单元,系统利用该基本单元实现系统对应程序的并发性。

不同点:

优缺点:

二、主线程和主队列

     主队列中的任务一定在主线程中执行
     主线程中执行的任务不一定在主队列中

示例一:
//给主队列设置标识
static void *key = @"MyQueue";
dispatch_queue_set_specific(dispatch_get_main_queue(), key, @"main", NULL);
//放到同步执行,全局并发队列中
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        //是否是主线程 0 不是 1 是
        NSLog(@"sync main thread:%d",[NSThread isMainThread]);
        //判断是否是主队列 0 不是 1 是
        void *value = dispatch_get_specific(key);//返回与当前分派队列关联的键的值。
        NSLog(@"sync main queue:%d",value != NULL);
 });

运行结果:

sync main thread:1//主线程
sync main queue:0//不是主队列

     众所周知,主队列是系统自动为我们创建的一个串行队列,因此不用我们手动创建。在每个应用程序,只有一个主队列,专门负责调度主线程里的任务不允许开辟新的线程
     上面的例子,是在主队列中调用『同步执行』+ 『全局并发队列』,因为是在『全局并发队列』中,所以block里执行的不是主队列
     因为是『同步执行』,不具备开启新线程的能力,所以是在主线程中。

     由上,我们可以知道,主线程中的任务,不一定是在主队列中

示例二:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //是否是主线程 0 不是 1 是
        NSLog(@"async main thread:%d",[NSThread isMainThread]);
        //判断是否是主队列 0 不是 1 是
        void *value = dispatch_get_specific(key);//返回与当前分派队列关联的键的值。
        NSLog(@"async main queue:%d",value != NULL);
 });

运行结果:

async main thread:0
async main queue:0

     『异步执行』+ 『全局并发队列』,因为不是在主队列中,而且异步执行中可以开辟一个线程,所以这里不是主线程也不是主队列。

示例三:
dispatch_async(dispatch_get_main_queue(), ^{
        //是否是主线程 0 不是 1 是
        NSLog(@"async main thread:%d",[NSThread isMainThread]);
        //判断是否是主队列 0 不是 1 是
        void *value = dispatch_get_specific(key);//返回与当前分派队列关联的键的值。
        NSLog(@"async main queue:%d",value != NULL);
});

运行结果:

async main thread:1
async main queue:1

     『异步执行』+ 『主队列』,回到主线程。

     由上,我们可以知道,主队列中的任务一定在主线程中执行

三、多线程

     同一时间内,单核CPU只能处理一条线程。多线程并发执行,其实是CPU快速地在多条线程之间调度(切换)。如果CPU调度线程的时间足够快,就造成了多线程并发执行的假象;

优点:

缺点:

四、主要有哪些多线程?

类型 简介 实现语言 线程生命周期 使用频率
NSThread 1、使用更加面向对象;
2、简单易用,可直接操作线程对象
OC 程序员管理 偶尔使用
GCD 1、旨在替代NSThread等线程技术;
2、充分利用设备的多核;
3、基于C的底层的API
C 自动管理 经常使用
NSOperation 1、基于GCD实现的Objective-C API;
2、比GCD多了一些更简单实用的功能;
3、使用更加面向对象
OC 自动管理 经常使用

NSThread

- (void)demoForNSThread {
    //方法一:需要start
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(testThread:) object:@"需要start"];
    //当使用初始化方法出来的主线程需要start启动
    [thread start];
    //为开辟的字线程起名字
    thread.name = @"NSThread线程";
    //调整权限,范围值为0~1。越大权限越高,先执行的概率越高。由于是概率,所以不是很准确的实现我们想要的执行顺序
    thread.threadPriority = 1;
    //取消当前启动的线程
    [thread cancel];
    
    //方法二:创建好之后自动启动,通过遍历构造器开辟子线程
    [NSThread detachNewThreadSelector:@selector(testThread:) toTarget:self withObject:@"构造器方式"];
    
    //方法三:隐式创建,直接启动,开辟子线程
    [self performSelectorInBackground:@selector(testThread:) withObject:@"隐式创建,直接启动"];
    //在当前线程,延迟1s执行。响应了oc语言的动态性:延迟到运行时才绑定方法
    [self performSelector:@selector(testThread:) withObject:@"在当前线程,延迟1s执行" afterDelay:1];
    /**
     回到主线程。waitUntilDone:是否将该方法执行完再执行后面的代码
     如果为Yes,就必须等testThread:执行完才能执行后面的代码,阻塞当前线程
     如果为No,不用等回调,直接执行,不阻塞当前线程
     */
    [self performSelectorOnMainThread:@selector(testThread:) withObject:@"回到主线程" waitUntilDone:YES];
    //在指定线程执行
    [self performSelector:@selector(testThread:) onThread:[NSThread currentThread] withObject:@"在指定线程执行" waitUntilDone:YES];
}

GCD

为什么要使用GCD?

GCD的相关使用

NSInvocationOperation
- (void)demoForNSInvocationOperation {
    
    NSLog(@"start---%@",[NSThread currentThread]);
    
    //1、创建 NSInvocationOperation 对象
    NSInvocationOperation *iop = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testNSOperation:) object:@"demoForNSInvocationOperation"];
    //2、调用 start 方法开始执行操作
    [iop start];
    
    NSLog(@"end---%@",[NSThread currentThread]);
}
start---<NSThread: 0x6000026581c0>{number = 1, name = main}
0---<NSThread: 0x6000026581c0>{number = 1, name = main}
1---<NSThread: 0x6000026581c0>{number = 1, name = main}
end---<NSThread: 0x6000026581c0>{number = 1, name = main}
NSBlockOperation(单个任务)
- (void)demoForNSBlockOperation {
    
    NSLog(@"start---%@",[NSThread currentThread]);
    
    //1、创建 NSBlockOperation 对象
    NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"%d---%@",i,[NSThread currentThread]);
        }
    }];
    
    //2、调用 start 方法开始执行操作
    [bop start];
    
    NSLog(@"end---%@",[NSThread currentThread]);
}
start---<NSThread: 0x6000005f41c0>{number = 1, name = main}
0---<NSThread: 0x6000005f41c0>{number = 1, name = main}
1---<NSThread: 0x6000005f41c0>{number = 1, name = main}
end---<NSThread: 0x6000005f41c0>{number = 1, name = main}

    我们可以看到使用NSInvocationOperationNSBlockOperation(只有一个任务)执行结果是一样的,在当前线程中同步执行,都是要等待执行任务回调完成后,才会继续执行后面的代码,会堵塞线程。

NSBlockOperation(多个任务)
- (void)demoForNSBlockOperationAddOperation {
    
    NSLog(@"start---%@",[NSThread currentThread]);
    
    //1、创建 NSBlockOperation 对象
    NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        }
    }];
    
    //2、添加额外操作
    [bop addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@",[NSThread currentThread]);
        }
    }];
    
    [bop addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@",[NSThread currentThread]);
        }
    }];
    
    [bop addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"4---%@",[NSThread currentThread]);
        }
    }];
    
    //3、调用 start 方法开始执行操作
    [bop start];
    
    NSLog(@"end---%@",[NSThread currentThread]);
}
start---<NSThread: 0x600001b001c0>{number = 1, name = main}
3---<NSThread: 0x600001b5af40>{number = 6, name = (null)}
4---<NSThread: 0x600001b14b40>{number = 5, name = (null)}
1---<NSThread: 0x600001b001c0>{number = 1, name = main}
2---<NSThread: 0x600001b157c0>{number = 7, name = (null)}
1---<NSThread: 0x600001b001c0>{number = 1, name = main}
3---<NSThread: 0x600001b5af40>{number = 6, name = (null)}
4---<NSThread: 0x600001b14b40>{number = 5, name = (null)}
2---<NSThread: 0x600001b157c0>{number = 7, name = (null)}
end---<NSThread: 0x600001b001c0>{number = 1, name = main}

    当多个任务时,额外操作都是在新开辟的子线程中运行的,系统会自动开启多个子线程去并发运行加入的block,开启的新线程数是由系统来决定

自定义NSOperation
- (void)demoForCustomNSOperation {
    
    NSLog(@"start---%@",[NSThread currentThread]);
    
    NSOperationQueue *oq = [NSOperationQueue new];
    
    NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        }
    }];

    [bop setCompletionBlock:^{
        NSLog(@"operation end --- %@",[NSThread currentThread]);
    }];
    [oq addOperation:bop];
    
    NSLog(@"end---%@",[NSThread currentThread]);
}
start---<NSThread: 0x600000d00980>{number = 1, name = main}
end---<NSThread: 0x600000d00980>{number = 1, name = main}
1---<NSThread: 0x600000d42640>{number = 4, name = (null)}
1---<NSThread: 0x600000d42640>{number = 4, name = (null)}
operation end --- <NSThread: 0x600002cf1d80>{number = 5, name = (null)}

      我们可以看到,这里虽然使用了NSBlockOperation,但是NSOperationQueue将它加入了子线程,让它不堵塞当前线程。
      这里重点说下NSOperation中的CompletionBlock,不论是在使用start直接调用,还是添加到NSOperationQueue,执行的内容都是在子线程中,而且CompletionBlock是在NSOperation执行完成后才执行(NSOperation的finished属性被KVO监听,如果一旦finished,就执行CompletionBlock)。有兴趣的同学可以试试看。

NSOperationQueue中有两种方式将Operation添加到队列中

- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;
- (void)demoForCustomNSOperationQueue {
    
    NSLog(@"start---%@",[NSThread currentThread]);
    
    NSOperationQueue *oq = [NSOperationQueue new];
    
    NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        }
    }];
    NSInvocationOperation *iop = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testNSOperation:) object:@""];
    
    [oq addOperation:bop];
    [oq addOperation:iop];
    [oq addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@",[NSThread currentThread]);
        }
    }];
    
    NSLog(@"end---%@",[NSThread currentThread]);
}
start---<NSThread: 0x6000026c8980>{number = 1, name = main}
end---<NSThread: 0x6000026c8980>{number = 1, name = main}
3---<NSThread: 0x6000026916c0>{number = 7, name = (null)}
1---<NSThread: 0x6000026c59c0>{number = 4, name = (null)}
2---<NSThread: 0x6000026844c0>{number = 5, name = (null)}
3---<NSThread: 0x6000026916c0>{number = 7, name = (null)}
1---<NSThread: 0x6000026c59c0>{number = 4, name = (null)}
2---<NSThread: 0x6000026844c0>{number = 5, name = (null)}

我们可以看到NSOperationQueue会异步开启新线程执行添加的Operation

总结一下

1.同步执行会在当前线程执行任务,不具备开启新线程的能力。并且必须等到Block函数执行完毕,dispatch函数才会返回,从而阻塞同一串行队列中外部方法的执行;

2.异步执行dispatch函数会直接返回,Block函数我们可以认为它会在下一帧加入队列,不会阻塞当前外部任务的执行。只有异步执行才有开辟新线程的必要,但是不一定会开辟新线程;

3.同步+串行:不开辟新线程,串行执行任务
   同步+并行:不开辟新线程,串行执行任务
   异步+串行:开辟一条新线程,串行执行任务
   异步+并行:开辟多条新线程,并行执行任务
   在主线程中同步使用主队列执行任务,会造成死锁

4.线程数量也不能无限开辟,线程的开辟同样会损耗资源,过多线程同时处理任务并不是想象中的人多力量大

参考:

iOS多线程:『GCD』详尽总结
iOS多线程编程
iOS之多线程漫谈
iOS多线程 -- NSOperation相关学习笔记

上一篇 下一篇

猜你喜欢

热点阅读