一、主队列介绍
主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行。提示:如果把任务放到主队列中进行处理,那么不论处理函数是异步的还是同步的都不会开启新的线程。
获取主队列的方式:
dispatch_queue_t queue = dispatch_get_main_queue();
(1)使用异步函数执行主队列中得任务,代码示例:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 开启子线程
[self performSelectorInBackground:@selector(asyncMain) withObject:nil];
}
/**
* 异步函数+主队列:不会开线程,在主线程中串行执行
*/
- (void)asyncMain
{
// 打印当前线程
NSLog(@"%s-----%@", __func__, [NSThread currentThread]);
// 获取主队列
dispatch_queue_t queue = dispatch_get_main_queue();
// 使用异步函数往主队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务1-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务2-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务3-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务4-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务5-----%@", [NSThread currentThread]);
});
}
@end
执行效果:
(2)使用同步函数,在主线程中执行主队列中得任务,会发生死循环,任务无法往下执行。示意图如下:
二、基本使用
1.问题
任务1和任务2是在主线程执行还是子线程执行,还是单独再开启一个新的线程?
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 开启一个后台线程,调用执行test方法
[self performSelectorInBackground:@selector(test) withObject:nil];
}
- (void)test
{
NSLog(@"当前线程---%@", [NSThread currentThread]);
// 获取全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 异步函数
dispatch_async(queue, ^{
NSLog(@"任务1所在线程-----%@", [NSThread currentThread]);
});
// 同步函数
dispatch_sync(queue, ^{
NSLog(@"任务2所在线程-----%@", [NSThread currentThread]);
});
}
@end
程序执行结果:
2.开启子线程,加载图片
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
// 当手指触摸屏幕的时候,从网络上下载一张图片显示到控制器的view上
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取一个全局串行队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 把任务添加到队列中执行
dispatch_async(queue, ^{
// 打印当前线程
NSLog(@"当前线程-----%@", [NSThread currentThread]);
// 从网络上下载图片
NSURL *url = [NSURL URLWithString:@"https://img-blog.csdn.net/20151125095714116?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// 提示
NSLog(@"图片下载完成");
// 回到主线程更新UI,显示图片
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
});
}
@end
显示效果:
打印结果:
要求使用GCD的方式,在子线程加载图片完毕后,主线程拿到加载的image刷新UI界面。
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
// 当手指触摸屏幕的时候,从网络上下载一张图片显示到控制器的view上
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取一个全局串行队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 把任务添加到队列中执行
dispatch_async(queue, ^{
// 打印当前线程
NSLog(@"当前线程-----%@", [NSThread currentThread]);
// 从网络上下载图片
NSURL *url = [NSURL URLWithString:@"https://img-blog.csdn.net/20151125095714116?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// 提示
NSLog(@"图片下载完成");
// 回到主线程更新UI,显示图片
//[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
// 打印当前线程
NSLog(@"刷新UI-----%@", [NSThread currentThread]);
});
});
}
@end
打印结果:
好处:子线程中得所有数据都可以直接拿到主线程中使用,更加的方便和直观.
三、线程间通信
从子线程回到主线程
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执⾏耗时的异步操作...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线程,执⾏UI刷新操作
});
});