iOS —— 多线程GCD

一、什么是 GCD

1. GCD 是苹果为解决多线程而定义的一套库,并且 GCD 可以自动管理线程的生命周期,就和 ARC 类似,不需要我们手动去管理

2. GCD 是用 纯C 语言 写的,所以我门使用的是 GCD 中的函数,并不是面向对象的方法

3. GCD 核心概念

  1)任务 : 就是某个线程要执行的方法

  2)队列 : 存放所有的任务

 

4. GCD 使用步骤

  1)确定要执行的任务

  2)将任务添加到队列中,GCD 会自动将队列中的任务取出,放在对应的线程中去执行

 

5. 同步异步

  1)同步 : 在同一个线程中执行任务,不会创建新的线程

// 同步函数
// 参数 1: 队列
// 参数 2: 任务的代码块
dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);

 

  2)异步 : 创建一个新的线程,并在新的线程中执行任务

// 异步函数
// 参数 1: 队列
// 参数 2: 任务的代码块
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

 

6. 队列

队列可分为两种

  1)异步队列 : 即并行执行的队列,队列中的每个任务都可以并发(同步)执行

  2)串行队列 : 即串行执行的队列,队列中的每个任务需要串行执行,即一个一个来

 

获得队列

// 创建串行队列
// 参数 1: 队列名称,C风格字符串
// 参数 2: 队列的属性,一般用 NULL 即可
dispatch_queue_t dispatch_queue_create(const char *_Nullable label, dispatch_queue_attr_t _Nullable attr);

dispatch_queue_t 是 GCD 中队列的类型

 

// 获得主队列,主队列是一个串行队列,并且与主线程对应,主队列中的任务都会被主线程执行
dispatch_queue_t dispatch_get_main_queue(void);

 

// 全局并发队列,可以供整个应用使用,不需手动创建
// 参数 1: 队列的优先级(有4个)
//    #define DISPATCH_QUEUE_PRIORITY_HIGH 2       高优先级
//    #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0  默认
//    #define DISPATCH_QUEUE_PRIORITY_LOW (-2)    低优先级
// 参数 2: 队列的属性,可以穿 0
dispatch_queue_t dispatch_get_global_queue(long identifier, unsigned long flags);    

 

二、GCD 基础应用

1. 异步/同步函数 与 串行/并行队列

1)使用异步函数向并发队列中添加任务

 

// 1. 打印主线程
NSLog(@"主线程 --- %@", [NSThread currentThread]);

// 2. 获取全局并发队列,并设置优先级为默认
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
// 3. 添加任务到并行队列中,就可以执行任务了
// 使用异步函数添加任务,可以开启新的线程
dispatch_async(queue, ^{
    
    NSLog(@"任务 1 --- %@", [NSThread currentThread]);
    
});
    
dispatch_async(queue, ^{
        
    NSLog(@"任务 2 --- %@", [NSThread currentThread]);
        
});
    
    
dispatch_async(queue, ^{
        
    NSLog(@"任务 3 --- %@", [NSThread currentThread]);
        
});

 

运行结果

 

总结 : 可以看出,除了主线程之外,还分别创建了三个子线程,并且三个子线程是并发执行的

 

2)使用异步函数向串行队列中添加任务

 

// 1. 创建串行队列
// 参数 1: 串行队列的名称,是 C风格字符串
// 参数 2: 串行队列的属性,一般来说串行队列是不需要任何属性,可以传 NULL
dispatch_queue_t queue = dispatch_queue_create("Chuanxin", NULL);
    
NSLog(@"主线程 --- %@", [NSThread currentThread]);
    
// 2. 使用异步函数往串行队列中添加任务
dispatch_async(queue, ^{
        
    NSLog(@"任务 1 --- %@", [NSThread currentThread]);
        
});
    
dispatch_async(queue, ^{
        
    NSLog(@"任务 2 --- %@", [NSThread currentThread]);
        
});
    
dispatch_async(queue, ^{
        
    NSLog(@"任务 3 --- %@", [NSThread currentThread]);
        
});

 

运行结果

 

总结 : 使用异步函数向串行队列中添加任务时,会开启新的线程,但是只会开启一个;因为串行队列中的任务需要一个一个执行,不必同时执行,所以只会创建一个新新线程

 

3)使用同步函数向并行队列中添加任务

 

NSLog(@"主线程 --- %@", [NSThread currentThread]);

// 1. 获取全局的并行队列,并设置优先级为 默认
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
// 2. 使用同步函数往并行队列中添加任务
dispatch_sync(queue, ^{
        
    NSLog(@"任务1 --- %@", [NSThread currentThread]);
        
});
    
dispatch_sync(queue, ^{
    
    NSLog(@"任务2 --- %@", [NSThread currentThread]);
    
});
    
dispatch_sync(queue, ^{
        
    NSLog(@"任务3 --- %@", [NSThread currentThread]);
        
});

 

运行结果

 

总结 : 因为使用的是同步函数,所以不会创建新的线程,所以都是在主线程中执行;此时,并发队列就失去了其功能,因为都没有新的线程创建,何谈并发

 

4)使用同步函数向串行队列中添加任务

 

 1 NSLog(@"主线程 --- %@", [NSThread currentThread]);
 2     
 3 // 1. 创建串行队列
 4 dispatch_queue_t queue = dispatch_queue_create("Chuanxing", NULL);
 5     
 6 // 2. 使用同步函数往串行队列中添加任务
 7 dispatch_sync(queue, ^{
 8         
 9     NSLog(@"任务1 --- %@", [NSThread currentThread]);
10         
11 });
12     
13 dispatch_sync(queue, ^{
14         
15     NSLog(@"任务2 --- %@", [NSThread currentThread]);
16         
17 });
18     
19 dispatch_sync(queue, ^{
20         
21     NSLog(@"任务2 --- %@", [NSThread currentThread]);
22         
23 });

 

运行结果

总结 : 因为使用的是同步函数,所以不会创建新线程,所以都在主线程中执行,并且是在串行队列中,所以任务会一个一个执行

 

2. 主队列 与 同步/异步

1)使用异步函数向主队列添加任务

 

 1 // 1. 获取主线程
 2 NSLog(@"mainThread --- %@", [NSThread currentThread]);
 3     
 4 // 1. 获取主队列
 5 dispatch_queue_t mainQueue = dispatch_get_main_queue();
 6     
 7 // 2. 使用异步函数向主队列中添加任务
 8 dispatch_async(mainQueue, ^{
 9         
10     NSLog(@"任务 1 --- %@", [NSThread currentThread]);
11         
12 });
13     
14 dispatch_async(mainQueue, ^{
15     
16     NSLog(@"任务 2 --- %@", [NSThread currentThread]);
17     
18 });
19     
20 dispatch_async(mainQueue, ^{
21         
22     NSLog(@"任务 3 --- %@", [NSThread currentThread]);
23         
24 });

 

运行结果

 

总结 : 虽然使用异步函数,但是却向主队列中添加任务,所以不会创建新的线程,都在主队列中执行任务,并且由于主队列是串行队列,所以任务会一个一个执行

 

2)使用同步函数向主队列中添加任务

 

// 1. 获取主线程
NSLog(@"mainThread --- %@", [NSThread currentThread]);
    
// 2. 获取主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
// 3. 使用同步方式向主队列中添加任务
dispatch_sync(mainQueue, ^{
        
    NSLog(@"任务 1 --- %@", [NSThread currentThread]);
        
});
    
dispatch_sync(mainQueue, ^{
        
    NSLog(@"任务 2 --- %@", [NSThread currentThread]);
        
});
    
dispatch_sync(mainQueue, ^{
        
    NSLog(@"任务 2 --- %@", [NSThread currentThread]);
        
});

 

运行结果

  使用同步函数向主队列添加任务时会使程序崩溃

  例如上述代码,当把 “任务1” 添加到主队列时,主队列变会让主线程执行该任务,但是此时主线程正在执行该同步函数,如此一来,便产生了一个死循环,导致死锁

 

 

 

 

3. 在子线程中创建子线程

 1 - (void)test3 {
 2     
 3     // 1. 获取当前线程(主线程)
 4     NSLog(@"currentThread --- %@", [NSThread currentThread]);
 5 
 6     // 2. 创建一个新的线程,并执行指定方法
 7     [self performSelectorInBackground:@selector(runInSubthread:) withObject:@"在子线程的子线程中执行任务"];
 8 
 9 }
10 
11 - (void)runInSubthread:(NSString *)str {
12 
13     // 1. 获取当前线程(子线程)
14     NSLog(@"currentThread --- %@ --- %@", [NSThread currentThread], str);
15     
16     // 2. 获取全局队列
17     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
18     
19     // 3. 使用异步函数向全局队列中添加任务(创建新的线程)
20     dispatch_async(globalQueue, ^{
21         
22         NSLog(@"任务 1 --- %@", [NSThread currentThread]);
23         
24     });
25     
26     // 4. 使用同步函数向全局队列中添加任务(在该线程中执行)
27     dispatch_sync(globalQueue, ^{
28     
29         NSLog(@"任务 2 --- %@", [NSThread currentThread]);
30     
31     });
32 
33 }

运行结果

 

总结 : 该程序共创建了 3 个线程,包括 : 主线程、performSelectorInBackground: withObject: 创建的线程,使用异步函数创建的线程

 

4. 加载图片

 1 #import "LHLoadImageViewViewController.h"
 2 
 3 @interface LHLoadImageViewViewController ()
 4 
 5 @property (nonatomic, strong) UIImageView * imageView;
 6 @property (nonatomic, strong) UIImage * image;
 7 
 8 @end
 9 
10 @implementation LHLoadImageViewViewController
11 
12 - (void)viewDidLoad {
13     
14     [super viewDidLoad];
15     
16     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 300, 600)];
17     
18     [self.view addSubview:_imageView];
19 
20 }
21 
22 
23 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
24 
25     // 1. 获取全局队列
26     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
27     
28     // 2. 使用异步函数将任务添加到全局队列中,即由子线程加载图片
29     dispatch_async(queue, ^{
30         
31         NSLog(@"currentThread --- %@", [NSThread currentThread]);
32         
33         // 1). 创建 URL
34         NSURL * url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/16/58/28/80M58PICTcs_1024.jpg"];
35         
36         // 2). 将 url 对应的内容转换为 NSData 数据对象
37         NSData * data = [NSData dataWithContentsOfURL:url];
38         
39         // 3). 用 NSData 数据对象的数据初始化 UIImage
40         _image = [UIImage imageWithData:data];
41         
42         NSLog(@"加载图片完成");
43         
44         // 4). 回到主线程刷新 UI
45         dispatch_async(dispatch_get_main_queue(), ^{
46             
47             NSLog(@"currentThread --- %@", [NSThread currentThread]);
48             
49             _imageView.image = _image;
50             
51         });
52         
53     });
54 
55 }
56 
57 
58 
59 @end

运行结果

总结 : 通常主线程用来刷新 UI 界面,而子线程用来做一些耗时的工作(加载图片等),从上述运行结果可以看出,加载图片由子线程执行,而刷新 UI 则有主线程执行

 

5. 延时方法 

前面说过的 sleepForTimeInterval: 方法 和 sleepUntilDate: 方法都是针对当前已经执行线程的,而本节所说的延时方法是针对还未执行的线程

1)使用 performSelector: withObject: afterDelay: 方法

使用该方法可以将指定的任务延迟多少时间(单位为秒)执行,并且该方法在哪个线程中被调用,那么指定的任务也就在哪个线程中执行

 1 - (void)test1 {
 2     
 3     // 1. 获取当前线程(主线程)
 4     NSThread * mainThread = [NSThread currentThread];
 5     
 6     NSLog(@"currentThread -- %@", mainThread);
 7     
 8     // 2. 延时 2s 调用(在本线程中)
 9     [self performSelector:@selector(run:) withObject:@"延时2s" afterDelay:2.0];
10 
11 }
12 
13 - (void)run:(NSString *)arg {
14 
15     // 1. 获取当前线程
16     NSThread * currentThread = [NSThread currentThread];
17     
18     NSLog(@"currentThread --- %@", currentThread);
19 
20 }

运行结果

总结 : 可以看出,因为调用 performSelector: withObject: afterDelay: 方法所在的线程为主线程,所以 run: 方法也在主线程中执行,并且延时了 2s

 

2)将 performSelector: withObject: afterDelay: 放在同步/异步函数中

 1 - (void)test2 {
 2     
 3     NSLog(@"currentThread --- %@", [NSThread currentThread]);
 4 
 5     // 1. 获取主队列
 6     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 7     
 8     // 2. 使用异步函数向全局队列添加任务
 9     dispatch_async(queue, ^{
10         
11         [self performSelector:@selector(run:) withObject:@"异步函数中执行任务" afterDelay:4.0];
12         
13     });
14      
15     
16     // 3. 使用同步函数向全局队列添加任务
17     dispatch_sync(queue, ^{
18         
19         [self performSelector:@selector(run:) withObject:@"同步函数中执行任务" afterDelay:4.0];
20         
21     });
22 
23 }
24 
25 - (void)run:(NSString *)arg {
26 
27     // 1. 获取当前线程
28     NSThread * currentThread = [NSThread currentThread];
29     
30     NSLog(@"currentThread --- %@ --- %@", currentThread, arg);
31 
32 }

运行结果

总结 : 可以看出,只有同步函数执行了任务,异步函数并没有

可见,将 performSelector: withObject: afterDelay: 方法 放在异步函数中是不起作用的

 

3)使用 dispatch_after 方法

// dispatch_after 函数
// 参数 1: 延时的时间
// 参数 2: 在哪个队列中执行
// 参数 3: 执行任务的代码块
dispatch_after(dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block);

dispatch_time_t 是表示时间类型,可以通过下面的函数创建

// dispatch_time 函数
// 参数 1: 从何时开始,一般用 DISPATCH_TIME_NOW 表示从当前开始
// 参数 2: 延时的秒数,单位为 纳秒
// 便于对参数 2 的方便使用,有定义以下宏
/* 注意,这三个宏的单位都是纳秒
#define NSEC_PER_SEC 1000000000ull   每秒有多少纳秒
#define USEC_PER_SEC 1000000ull        每秒有多少毫秒
#define NSEC_PER_USEC 1000ull           每毫秒有多少纳秒
*/
dispatch_time_t dispatch_time(dispatch_time_t when, int64_t delta);

延时 1s 将任务放到主队列中的代码如下

 1 dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
 2     
 3 NSLog(@"延迟开始");
 4     
 5 dispatch_after(time, dispatch_get_main_queue(), ^{
 6         
 7     NSLog(@"执行任务中...");
 8         
 9     NSLog(@"延迟结束");
10 });

运行结果

 

posted @ 2017-04-19 10:18  透心凉mmm  阅读(312)  评论(0编辑  收藏  举报