一.主队里介绍
主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中的任务,都会当到主线程中执行。
提示:如果把任务放倒主队列中进行处理,那么无论处理函数是异步的还是同步的都不会开启新的线程。
获取主队列的方式:
dispatch_queue_t queue=dispatch_get_main_queue();
(1)使用异步函数执行主队列的任务

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) NSThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

    //获取主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //2.把任务添加到主队列中执行
    dispatch_async(queue, ^{
        NSLog(@"使用异步函数执行主队列中的任务1--%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"使用异步函数执行主队列中的任务2--%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"使用异步函数执行主队列中的任务3--%@",[NSThread currentThread]);
    });

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

(2)使用同步函数,在主线程中执行主队列中得任务,会发生死循环,任务无法往下执行。示意图如下:

iostat查阻塞线程 ios阻塞主线程_主线程

二.基本使用
1.问题
任务1和任务2是在主线程执行还是子线程执行,还是单独再开启一个新的线程?

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) NSThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //打印主线程
    NSLog(@"主线程--%@", [NSThread mainThread]);
    //开启一个后台线程,调用执行test方法
    [self performSelectorInBackground:@selector(test) withObject:nil];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (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

答案:任务1是在单独开启的线程中执行的。任务2是在子线程中执行的。

2.开启子线程,加载图片

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) UIImageView *iconView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _iconView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_iconView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取一个全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //把任务添加到队列中执行
    dispatch_async(queue, ^{
        //打印当前线程
        NSLog(@"%@",[NSThread currentThread]);
        //从网络上下载图片
        NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:urlstr];
        UIImage *image = [UIImage imageWithData:data];
        //提示
        NSLog(@"图片加载完毕");
        //回到主线程,展示图片
        [self.iconView performSelectorOnMainThread:@selector(setImage:)
                                        withObject:image
                                     waitUntilDone:NO];
    });
}

@end

要求使用GCD的方式,在子线程加载图片完毕后,主线程拿到加载的image刷新UI界面。

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) UIImageView *iconView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _iconView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_iconView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取一个全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //把任务添加到队列中执行
    dispatch_async(queue, ^{
        //打印当前线程
        NSLog(@"%@",[NSThread currentThread]);
        //从网络上下载图片
        NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:urlstr];
        UIImage *image = [UIImage imageWithData:data];
        //提示
        NSLog(@"图片加载完毕");
        //回到主线程,展示图片
//        [self.iconView performSelectorOnMainThread:@selector(setImage:)
//                                        withObject:image
//                                     waitUntilDone:NO];
        dispatch_async(dispatch_get_main_queue(), ^{
            _iconView.image = image;
            //打印当前线程
            NSLog(@"%@",[NSThread currentThread]);
        });
    });
}

@end

好处:子线程中得所有数据都可以直接拿到主线程中使用,更加的方便和直观。

三.线程间通信
从子线程回到主线程

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //执⾏耗时的异步操作...
    dispatch_async(dispatch_get_main_queue(), ^{
        //回到主线程,执⾏UI刷新操作
    });
});