iOS GCD 多核编程(dispatch_async、dispatch_queue)

GCD(Grand Central Dispatch)是Apple开发的一个多核编程的解决方案

dispatch queue分成以下3种 

 (1)运行在主线程的”Main queue“,通过dispatch_get_main_queue获取 

 (2)并行队列“global disppatch queue”,通过dispatch_get_global_queue获取。并行队列的执行顺序与其加入队列的顺序相同

 (3)串行队列”serial queue“一般用于按顺序同步访问。可以创建任意数量的串行队列,各个串行队列之间是并发的。通过dispatch_queue_create创建,可以通过食用dispatch_retain和dispatch_release去增加或减少引用计数。

 在没有GCD时,当app按home键退出后,app仅有最多5秒钟的时间做一些保存或清理资源的工作。但是在使用GCD后,app最多有10分钟的时间在后台长久运行。

//
//  ViewController.m
//  GCDDemo
//
//  Created by 555chy on 6/25/16.
//  Copyright © 2016 555chy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self logThreadInfo:@"main"];
    /*
     dispatch_queue_t dispatch_get_global_queue(long identifier, unsigned long flags);
     * @param identifier
     * A quality of service class defined in qos_class_t or a priority defined in
     * dispatch_queue_priority_t.
     *
     * It is recommended to use quality of service class values to identify the
     * well-known global concurrent queues:
     *  - QOS_CLASS_USER_INTERACTIVE
     *  - QOS_CLASS_USER_INITIATED
     *  - QOS_CLASS_DEFAULT
     *  - QOS_CLASS_UTILITY
     *  - QOS_CLASS_BACKGROUND
     *
     * The global concurrent queues may still be identified by their priority,
     * which map to the following QOS classes:
     *  - DISPATCH_QUEUE_PRIORITY_HIGH:         QOS_CLASS_USER_INITIATED
     *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:      QOS_CLASS_DEFAULT
     *  - DISPATCH_QUEUE_PRIORITY_LOW:          QOS_CLASS_UTILITY
     *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND:   QOS_CLASS_BACKGROUND
     *
     * @param flags
     * Reserved for future use. Passing any value other than zero may result in
     * a NULL return value.
     */
    //主线程运行
    dispatch_async(dispatch_get_main_queue(), ^{
        [self logThreadInfo:@"dispatch_async(dispatch_get_main_queue()"];
    });
    //后台运行
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
        [self logThreadInfo:@"dispatch_async(dispatch_get_global_queue(0,0)"];
    });
    //一次性执行,dispatch_once可以保证后面block函数内部的代码即使在循环中也只会被执行一次
    static dispatch_once_t onceToken;
    for(int i=0; i<5; i++) {
        dispatch_once(&onceToken, ^{
            [self logThreadInfo:[NSString stringWithFormat:@"dispatch_once, index = %d", i]];
        });
    }
    //延迟2秒执行
    double delayInSeconds = 2.0;
    dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(dispatchTime, dispatch_get_main_queue(), ^{
        [self logThreadInfo:@"dispatch_after(dispatchTime, dispatch_get_main_queue()"];
    });
    //自定义dispatch_queue_t
    const char *diyDispatchQueueLabel = "diyDispatchQueueLabel";
    dispatch_queue_t diyDispatchQueue = dispatch_queue_create(diyDispatchQueueLabel, NULL);
    dispatch_async(diyDispatchQueue, ^{
        [self logThreadInfo:@"dispatch_queue_create(diyDispatchQueueLabel, NULL)"];
    });
    //合并汇总结果
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_group_async(dispatchGroup, dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
        [self logThreadInfo:@"dispatch_group_async 1"];
    });
    dispatch_group_async(dispatchGroup, dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
        [self logThreadInfo:@"dispatch_group_async 2"];
    });
    dispatch_group_notify(dispatchGroup, dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
        //dispatch_group_notify会等队列中的所有操作执行完毕之后才执行
        [self logThreadInfo:@"dispatch_group_notify"];
    });
}

-(void)logThreadInfo:(NSString *)what {
    NSThread *thread = [NSThread currentThread];
    NSLog(@"%@ is %@, isMainThread = %d", what, thread, [thread isMainThread]);
}

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

@end

运行截图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值