GCD

//

//  ViewController.m

//  GCDThread

//

//  Created by 李洪鹏 on 15/7/29.

//  Copyright (c) 2015 李洪鹏. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //GCD Grand central dispatch

    /**

     *  ios里面实现多线程的技术有很多,其中使用起来最简单的是GCD, 执行效果效率最高的是GCD 是相对底层的API 全部由C语言实现,通过队列和任务的形式实现多线程

     *

     *  dispatch queue, 它有两种队列, 1.serial (串行队列) 2. concurrent (并行队列)

     */

    

}

 

//serial队列特点:队列里面的任务按照顺序依次完成,一个执行完成再去执行下一次,而且serial队列有两种创建方式

 

#pragma mark -------serial第一种创建方式

- (IBAction)serial_mainSerialButton:(UIButton *)sender

{

    dispatch_queue_t serialOneQueue = dispatch_get_main_queue();  // 主队列

    

    //往队列里面添加任务

   // dispatch_sync(<#dispatch_queue_t queue#>, <#^(void)block#>)

    //执行完block内部的代码再去执行

   

    

    // dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)

   //不等block体执行完毕,就去执行外面的代码

    

    //添加任务

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"0个任务%@", [NSThread currentThread]);

        

    });

 

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"1个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"2个任务%@", [NSThread currentThread]);

        

    });

 

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"3个任务%@", [NSThread currentThread]);

        

    });

    

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"4个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"5个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"6个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"7个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"8个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(serialOneQueue, ^{

        

        NSLog(@"9个任务%@", [NSThread currentThread]);

        

    });

 

 

 

    

}

 

 

#pragma mark ----serial 队列的方式

- (IBAction)serialUserSerialButton:(UIButton *)sender {

    

    //第一个参数 队列的名字

    //第二个参数 是串行还是并行

    dispatch_queue_t queue = dispatch_queue_create("com.lhp", DISPATCH_QUEUE_SERIAL);

    

    //添加任务

    dispatch_async(queue, ^{

        

        NSLog(@"0个任务%@", [NSThread currentThread]);

        

    });

    

    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]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"6个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"7个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"8个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"9个任务%@", [NSThread currentThread]);

        

    });

    

    

    

}

 

//并行队列特点. 按照顺序开始执行  但是第二个任务不会等第一个任务结束才开始, 最终结果是队列里面的任务执行完成的顺序可能会跟进入队列的顺序不一致

#pragma mark------并行队列的第一种方式(全局队列 -GCD提供了三个全局队列)

- (IBAction)GCD_conCurrentButton:(UIButton *)sender {

    

    //获取全局队列 (并行队列)

    //第一个参数 ,队列的优先级, 共有四个优先级, background   low default  heigh

    //第二个参数 苹果预留的参数  目前没用 0

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    //添加任务

    dispatch_async(queue, ^{

        

        NSLog(@"0个任务%@", [NSThread currentThread]);

        

    });

    

    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]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"6个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"7个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"8个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"9个任务%@", [NSThread currentThread]);

        

    });

    

}

 

 

#pragma mark-----并行队列的第二种方式

 

- (IBAction)GCD_conCurrentButton2:(UIButton *)sender

{

    

    dispatch_queue_t queue = dispatch_queue_create("com.lhp", DISPATCH_QUEUE_CONCURRENT);

    

    //添加任务

    dispatch_async(queue, ^{

        

        NSLog(@"0个任务%@", [NSThread currentThread]);

        

    });

    

    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]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"6个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"7个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"8个任务%@", [NSThread currentThread]);

        

    });

    dispatch_async(queue, ^{

        

        NSLog(@"9个任务%@", [NSThread currentThread]);

        

    });

 

    

}

 

//延迟执行某一个方法

- (IBAction)GCD_delayButton:(UIButton *)sender {

//    

//    double timeDelay = 5.0;

//    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW,(int64_t) timeDelay * NSEC_PER_SEC);

//    

//    //开始

//    dispatch_after(time, dispatch_get_main_queue(), ^{

//        

//        NSLog(@"我被延迟了5s");

//    });

    

    //第二种方式

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        

        NSLog(@"我被延迟了三秒");

    });

    

}

 

//GCD  单次执行某个任务,这个任务在程序期间运行执行一次,而且保证这个任务在多线程的情况下是安全的

- (IBAction)GCD_onceButton:(UIButton *)sender

{

    

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"我的第一次");

    });

    

}

 

- (IBAction)GCD_groupButton:(UIButton *)sender {

    

    //创建一个组

//    dispatch_group_t  把不同的任务归为一个小组

//    dispatch_group_notify 组里面的任务执行完毕,才能执行dispatch_group_notify 的任务 

 

    dispatch_group_t group =dispatch_group_create();

    

    dispatch_queue_t queue = dispatch_queue_create("lhp", DISPATCH_QUEUE_CONCURRENT);

    

    //添加任务

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"第一个任务%@", [NSThread currentThread]);

    });

 

    dispatch_group_async(group, queue, ^{

    

    NSLog(@"第二个任务%@", [NSThread currentThread]);

    });

 

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"第三个任务%@", [NSThread currentThread]);

    });

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"第四个任务%@", [NSThread currentThread]);

    });

 

    dispatch_group_async(group, queue, ^{

    

    NSLog(@"第五个任务%@", [NSThread currentThread]);

    });

    

    //通过notify 通过添加组

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"我是group 组里面所有任务执行完毕之后才执行的任务");

    });

    

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"6个任务%@", [NSThread currentThread]);

    });

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"7个任务%@", [NSThread currentThread]);

    });

    

    dispatch_group_async(group, queue, ^{

        

        NSLog(@"8个任务%@", [NSThread currentThread]);

    });

 

    

    

 

}

 

 

 

- (IBAction)GCD_barrierButton:(UIButton *)sender

{

    //

    dispatch_queue_t queue = dispatch_queue_create("com.lhp", DISPATCH_QUEUE_CONCURRENT);

    

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

 

    

    

    //往数据库里面写入内容

    dispatch_barrier_sync(queue, ^{

        NSLog(@"往数据库里面写入内容所在线程%@", [NSThread currentThread]);

        

    });

  

    

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    dispatch_async(queue, ^{

        NSLog(@"读取数据库内容所在线程 %@", [NSThread currentThread]);

    });

    

    

}

 

 

 

//某个任务执行若干次

- (IBAction)GCD_applyButton:(UIButton *)sender

{

    NSArray *array = @[@"杨过",@"小龙女", @"尹志平", @"大雕", @"卡布达"];

    

    //创建一个队列

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

//    block里面的任务执行多次

    dispatch_apply(array.count, queue, ^(size_t index) {

        

        NSLog(@"%@", array[index]);

        

    });

    

    

}

 

 

- (IBAction)GCD_functionButton:(UIButton *)sender {

    

    //创建队列

    dispatch_queue_t queue = dispatch_queue_create("com.lhp", DISPATCH_QUEUE_CONCURRENT);

    

    //往队列里面添加任务

    

//   dispatch_async_f 往队列里面添加任务不再是控制block的执行 ,而是控制函数的执行, 而且这个函数是有特殊要求的,返回值必须是 void 参数必须是 void*

    dispatch_async_f(queue, @"hello world", function);

    

    //context 第二个参数可以是任何对象类型的参数

    

}

 

//C语言的函数

void function(void *context)

{

    

    NSLog(@"%@ %@", [NSThread currentThread], context);

    

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

原文地址:https://www.cnblogs.com/lhp-1992/p/4687199.html