ios多线程之GCD

  ios中得多线程技术主要使用3种:NSThread、NSOperation和GCD,这里主要讲GCD

  GCD:(Grand Central Dispatch)是一种多核编码技术,用纯C语言编写。

  异步:具备开启线程的功能

  同步:不具备开启线程的功能

  并行队列:多个任务可以同时执行

  串行队列:执行完一个任务后再执行下一个任务

 

  下面来进行验证:

 1 // 自定义异步方法
 2 - (void)async:(dispatch_queue_t)queue
 3 {
 4     // 异步执行
 5     dispatch_async(queue, ^{
 6         
 7         NSLog(@"im1-%@",[NSThread currentThread]);
 8     });
 9     dispatch_async(queue, ^{
10         
11         NSLog(@"im2-%@",[NSThread currentThread]);
12     });
13     dispatch_async(queue, ^{
14         
15         NSLog(@"im3-%@",[NSThread currentThread]);
16     });
17     dispatch_async(queue, ^{
18         
19         NSLog(@"im4-%@",[NSThread currentThread]);
20     });
21 
22 }

 

 1 // 自定义同步方法
 2 - (void)sync:(dispatch_queue_t)queue
 3 {
 4     // 同步执行
 5     dispatch_sync(queue, ^{
 6         
 7         NSLog(@"im1-%@",[NSThread currentThread]);
 8     });
 9     dispatch_sync(queue, ^{
10         
11         NSLog(@"im2-%@",[NSThread currentThread]);
12     });
13     dispatch_sync(queue, ^{
14         
15         NSLog(@"im3-%@",[NSThread currentThread]);
16     });
17     dispatch_sync(queue, ^{
18         
19         NSLog(@"im4-%@",[NSThread currentThread]);
20     });
21 
22 }

 

调用异步执行方法:

1 // 创建一个全局并行队列
2     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
3     
4     // 异步执行
5     [self async:queue];

最终的结果为:开启了四个线程,并且执行顺序是不定

1 2015-04-02 16:29:20.438 GCD[4398:1f07] im3-<NSThread: 0x7177ee0>{name = (null), num = 5}
2 2015-04-02 16:29:20.432 GCD[4398:1303] im1-<NSThread: 0x7177d70>{name = (null), num = 3}
3 2015-04-02 16:29:20.440 GCD[4398:4307] im4-<NSThread: 0x7178490>{name = (null), num = 6}
4 2015-04-02 16:29:20.432 GCD[4398:1a03] im2-<NSThread: 0x75429f0>{name = (null), num = 4}

 

1 // 创建一个全局串行队列
2     dispatch_queue_t queue = dispatch_queue_create("wys", NULL);
3     
4     // 异步执行
5     [self async:queue];

最终的结果为:开启了一个线程,执行顺序为从上往下依次执行

1 2015-04-02 16:32:19.415 GCD[4442:1303] im1-<NSThread: 0x71631a0>{name = (null), num = 3}
2 2015-04-02 16:32:19.428 GCD[4442:1303] im2-<NSThread: 0x71631a0>{name = (null), num = 3}
3 2015-04-02 16:32:19.437 GCD[4442:1303] im3-<NSThread: 0x71631a0>{name = (null), num = 3}
4 2015-04-02 16:32:19.450 GCD[4442:1303] im4-<NSThread: 0x71631a0>{name = (null), num = 3}

 

 

调用主队列执行方法:

1 // 创建主队列
2     dispatch_queue_t queue = dispatch_get_main_queue();
3     
4     // 异步执行
5     [self async:queue];

最红的结果为:顺序执行并且不开启线程,在主线程中执行

1 2015-04-02 16:35:45.320 GCD[4484:c07] im1-<NSThread: 0x71560c0>{name = (null), num = 1}
2 2015-04-02 16:35:45.333 GCD[4484:c07] im2-<NSThread: 0x71560c0>{name = (null), num = 1}
3 2015-04-02 16:35:45.339 GCD[4484:c07] im3-<NSThread: 0x71560c0>{name = (null), num = 1}
4 2015-04-02 16:35:45.347 GCD[4484:c07] im4-<NSThread: 0x71560c0>{name = (null), num = 1}

 

1 // 创建主队列
2     dispatch_queue_t queue = dispatch_get_main_queue();
3     
4     NSLog(@"start");
5     
6     // 同步执行
7     [self sync:queue];
8     
9     NSLog(@"end");

最终的结果为:执行到start就卡住了,不能往下执行

1 2015-04-02 16:38:12.856 GCD[4514:c07] start

 

 

调用同步执行方法:

1 // 创建全局并行队列
2     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
3     
4     // 同步执行
5     [self sync:queue];

最终的结果为:不开启线程,并且顺序执行,直接主线程执行

1 2015-04-02 16:41:27.448 GCD[4555:c07] im1-<NSThread: 0x71133c0>{name = (null), num = 1}
2 2015-04-02 16:41:27.458 GCD[4555:c07] im2-<NSThread: 0x71133c0>{name = (null), num = 1}
3 2015-04-02 16:41:27.468 GCD[4555:c07] im3-<NSThread: 0x71133c0>{name = (null), num = 1}
4 2015-04-02 16:41:27.472 GCD[4555:c07] im4-<NSThread: 0x71133c0>{name = (null), num = 1}

 

1 // 创建串行队列
2     dispatch_queue_t queue = dispatch_queue_create("wys", NULL);
3     
4     // 同步执行
5     [self sync:queue];

最终的结果为:不开启线程,并且顺序执行,直接主线程执行

1 2015-04-02 16:43:40.609 GCD[4589:c07] im1-<NSThread: 0x713e570>{name = (null), num = 1}
2 2015-04-02 16:43:40.621 GCD[4589:c07] im2-<NSThread: 0x713e570>{name = (null), num = 1}
3 2015-04-02 16:43:40.626 GCD[4589:c07] im3-<NSThread: 0x713e570>{name = (null), num = 1}
4 2015-04-02 16:43:40.634 GCD[4589:c07] im4-<NSThread: 0x713e570>{name = (null), num = 1}

 

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。