GCD关于队列和函数对于调用线程的影响

来源:互联网 发布:电脑整蛊软件 编辑:程序博客网 时间:2024/06/05 00:34

Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,它的推出是为了取代繁琐的NSThread,它能够自动利用更多的CPU的核数,可以自动管理线程的生命周期,将程序员从繁琐复杂的线程管理中解脱出来。利用GCD技术,我们只需要告诉GCD想要执行的任务就行了。

GCD中有两个核心的概念:

  • "队列"

所谓队列则是用来“存放”任务的。队列和线程是两个概念。队列中存放的任务最后都得由线程来执行!队列的原则:先进先出(FIFO/First In First Out)!

队列大致有几种类型:

常见类型:

1.并行队列(Concurrent Dispatch Queue):并行队列中存放的任务是想要按同时执行(不一定可以同时执行哦

创建一个并行队列

 dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);

2.串行队列(Serial Dispatch Queue):串行队列中存放的任务是想要按顺序执行(不一定可以按照顺序执行哦

创建一个串行队列

<span style="font-family:Verdana, Lucida Grande, Arial, Helvetica, sans-serif;">  dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);</span>

特殊类型:

3.主队列:跟主线程相关联的队列,是GCD自带的一种特殊的串行队列,它里面存放的任务都是在主线程中按顺序执行的

主队列只能获取不能创建

 dispatch_queue_t mainQueue = dispatch_get_main_queue();

4.全局并发队列:一般情况下,并发任务都是存放在全局并发队列中!

全局队列也是只能获取不能创建的

 dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);

  • "任务"

所谓任务就是想要做的事情,执行的操作;GCD中的任务定义在block中。

<span style="font-family:Verdana, Lucida Grande, Arial, Helvetica, sans-serif;">void (^myBlock)() = ^{// 想要做的任务}</span>

GCD提供了两种执行任务的函数

1.‘同步’执行任务:同步函数不具备开启新线程的能力,它只能当前线程执行block中的代码

 dispatch_sync(dispatch_queue_t queue, ^(void)block)
2.‘异步’执行任务:异步函数具备开启新线程的能力,可以在新的线程中执行任务

 dispatch_async(dispatch_queue_t queue, ^(void)block)

下面我们分别利用代码来验证函数和队列对于线程的使用规则

A.串行队列同步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self serial_queue_with_sync];}- (void)serial_queue_with_sync{    NSLog(@"%@",[NSThread currentThread]);     dispatch_queue_t serial_queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);    dispatch_sync(serial_queue, ^{        NSLog(@"串行队列➕同步函数1%@",[NSThread currentThread]);    });    dispatch_sync(serial_queue, ^{        NSLog(@"串行队列➕同步函数2%@",[NSThread currentThread]);    });    dispatch_sync(serial_queue, ^{        NSLog(@"串行队列➕同步函数3%@",[NSThread currentThread]);    });    dispatch_sync(serial_queue, ^{        NSLog(@"串行队列➕同步函数4%@",[NSThread currentThread]);    });}

运行结果如下图所示,可以看到串行队列和同步函数只能在当前线程按顺序执行



B.串行队列异步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self serial_queue_with_async];}-  (void)serial_queue_with_async{    NSLog(@"%@",[NSThread currentThread]);    //    创建一个串行队列    dispatch_queue_t serial_queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);    //    异步函数    dispatch_async(serial_queue, ^{        NSLog(@"串行队列➕异步函数1%@",[NSThread currentThread]);    });    dispatch_async(serial_queue, ^{        NSLog(@"串行队列➕异步函数2%@",[NSThread currentThread]);    });    dispatch_async(serial_queue, ^{        NSLog(@"串行队列➕异步函数3%@",[NSThread currentThread]);    });    dispatch_async(serial_queue, ^{        NSLog(@"串行队列➕异步函数4%@",[NSThread currentThread]);    });    dispatch_async(serial_queue, ^{        NSLog(@"串行队列➕异步函数5%@",[NSThread currentThread]);    });}
运行结果如下图所示,可以看到串行队列和异步函数在子线程按顺序执行


C.并发队列同步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self concunrrent_queue_with_sync];}- (void)concunrrent_queue_with_sync{    NSLog(@"%@",[NSThread currentThread]);    dispatch_queue_t concunrrent_queue = dispatch_queue_create("concunrrent",DISPATCH_QUEUE_CONCURRENT);    dispatch_sync(concunrrent_queue, ^{        NSLog(@"并发队列➕同步函数1%@",[NSThread currentThread]);    });    dispatch_sync(concunrrent_queue, ^{           NSLog(@"并发队列➕同步函数2%@",[NSThread currentThread]);    });    dispatch_sync(concunrrent_queue, ^{        NSLog(@"并发队列➕同步函数3%@",[NSThread currentThread]);    });    dispatch_sync(concunrrent_queue, ^{        NSLog(@"并发队列➕同步函数4%@",[NSThread currentThread]);    });}

运行结果如下图所示,可以看到并发队列和同步函数在不开启新线程,在当前线程按顺序执行


D.并发队列异步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self concunrrent_queue_with_async];}- (void)concunrrent_queue_with_async{    NSLog(@"%@",[NSThread currentThread]);    dispatch_queue_t concunrrent_queue = dispatch_queue_create("concunrrent",DISPATCH_QUEUE_CONCURRENT);    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列1%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{         NSLog(@"异步函数➕并发队列2%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列3%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列4%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列5%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列6%@",[NSThread currentThread]);    });    dispatch_async(concunrrent_queue, ^{        NSLog(@"异步函数➕并发队列7%@",[NSThread currentThread]);    });}
运行结果如下图所示;可以观察到异步函数和并发队列会开启新线程,无序的,同时执行任务


E.主队列异步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self main_queue_with_async];}- (void)main_queue_with_async{     NSLog(@"%@",[NSThread currentThread]);    dispatch_async(dispatch_get_main_queue(), ^{        NSLog(@"主队列➕异步函数1%@",[NSThread currentThread]);    });    dispatch_async(dispatch_get_main_queue(), ^{        NSLog(@"主队列➕异步函数2%@",[NSThread currentThread]);    });    dispatch_async(dispatch_get_main_queue(), ^{        NSLog(@"主队列➕异步函数3%@",[NSThread currentThread]);    });    dispatch_async(dispatch_get_main_queue(), ^{        NSLog(@"主队列➕异步函数4%@",[NSThread currentThread]);    });}
运行结果如下图所示;可以观察到异步函数和主队列不会开启新线程,主线程按顺序执行任务。



F.主队列同步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self main_queue_with_sync];}- (void)main_queue_with_sync{    NSLog(@"hahaha");    dispatch_sync(dispatch_get_main_queue(), ^{        NSLog(@"%@",[NSThread currentThread]);    });}
运行结果如下图所示;可以观察到同步函数和主队列会阻塞线程,不能执行任务


G.全局并发队列同步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self global_queue_with_sync];}- (void)global_queue_with_sync{    NSLog(@"%@",[NSThread currentThread]);    dispatch_queue_t global_queue = dispatch_get_global_queue(0, 0);    dispatch_sync(global_queue, ^{        NSLog(@"全局队列➕同步函数1%@",[NSThread currentThread]);    });    dispatch_sync(global_queue, ^{        NSLog(@"全局队列➕同步函数2%@",[NSThread currentThread]);    });    dispatch_sync(global_queue, ^{        NSLog(@"全局队列➕同步函数3%@",[NSThread currentThread]);    });    dispatch_sync(global_queue, ^{        NSLog(@"全局队列➕同步函数4%@",[NSThread currentThread]);    });}
运行结果如下图所示;可以观察到同步函数和全局并发队列不会开启新线程,当前线程按顺序执行任务

H.全局队列异步函数

- (void)viewDidLoad {    [super viewDidLoad];    [self global_queue_with_async];}- (void)global_queue_with_async{    NSLog(@"%@",[NSThread currentThread]);    dispatch_queue_t global_queue = dispatch_get_global_queue(0, 0);    dispatch_async(global_queue, ^{        NSLog(@"全局队列➕异步函数1%@",[NSThread currentThread]);    });    dispatch_async(global_queue, ^{        NSLog(@"全局队列➕异步函数2%@",[NSThread currentThread]);    });    dispatch_async(global_queue, ^{        NSLog(@"全局队列➕异步函数3%@",[NSThread currentThread]);    });    dispatch_async(global_queue, ^{        NSLog(@"全局队列➕异步函数4%@",[NSThread currentThread]);    });    dispatch_async(global_queue, ^{        NSLog(@"全局队列➕异步函数5%@",[NSThread currentThread]);    });}

运行结果如下图所示;可以观察到异步函数和全局并发队列会开启新线程,在新开启的线程中同时执行任务



0 0
原创粉丝点击