美文网首页
iOS开发中的四种GCD

iOS开发中的四种GCD

作者: BrianWang | 来源:发表于2019-01-23 13:59 被阅读0次
    //
    //  ViewController.m
    //  GCDDemo1
    //
    //  Created by WangZhi on 16/6/3.
    //  Copyright © 2016年 WangZhi. 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.
        
        NSLog(@"%@", [NSThread currentThread]);
        
        // GCD核心:将 操作/任务 放到 队列 中
        
        //    [self GCD1];
        //    [self GCD2];
        //    [self GCD3];
        [self GCD4];
    }
    ​
    
    #pragma mark - 串行队列
    
    - (void)GCD1 {
        // 特点: 可以保证效率(会新建一个子线程),能够实现并发! (因为新建子线程是有开销的,所以不能无休止的新建线程.)
        // 使用串行队列的异步任务非常非常非常有用!!!
        // 应用场景: 从网络上下载图片,然后加滤镜处理
        // 在C语言函数中,定义类型绝大多数都是以_t或ref结尾的.
        
        dispatch_queue_t q = dispatch_queue_create("Queue1", DISPATCH_QUEUE_SERIAL); // 串行队列
    
        // 非ARC开发环境中,千万别忘记release
        // dispatch_release(q);
    
        // 1. 串行队列的同步任务
        for (int i = 0; i < 10; i++) {
            dispatch_sync(q, ^{ // 同步任务,顺序执行,会在主线程上运行(在开发中极少用)
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
        
        // 2. 串行队列的异步任务
        for (int i = 0; i < 10; i++) {
            dispatch_async(q, ^{ // 异步任务,顺序执行,会在子线程上运行(会新建一个子线程) (异步任务,并发执行,但如果在串行队列中,仍然会依次顺序执行)
                // [NSThread currentThread]:可以在开发中,跟踪当前线程
                // number = 1,表示主线程; number = 2,表示第二个子线程,以此类推.
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
    }
    
    
    #pragma mark - 并行队列
    
    - (void)GCD2 {
        // 特点: 没有队形,执行顺序程序员不能控制!!!
        // 缺点: 并行队列易出错,并且不能控制新建子线程的数量.
        // 应用场景: 并发执行任务,没有先后顺序
        dispatch_queue_t q = dispatch_queue_create("Queue2", DISPATCH_QUEUE_CONCURRENT); // 并行队列
    
        // 1. 并行队列的同步任务
        for (int i = 0; i < 10; i++) {
            dispatch_sync(q, ^{ // 同步任务,顺序执行,会在主线程上运行
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
    
        // 2. 并行队列的异步任务
        for (int i = 0; i < 10; i++) {
            dispatch_async(q, ^{ // 异步任务,并发执行,会在子线程上运行(可能会新建多个子线程)
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
    }
    
    
    #pragma mark - 全局队列:苹果为了方便多线程的设计,提供了一个全局队列,供所有的APP共同使用
    
    - (void)GCD3 { 
         // 全局队列与并行队列的区别:
         // - 不需要创建,直接get就可以获得;
         // - 全局队列没有名称,调试时,无法确认准确队列;
         // - 与并行队列执行效果相同,属于并行队列类型.
    
        dispatch_queue_t global_q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 第二个参数flags传0,原因:Reserved for future use. Passing any value other than zero may result in a NULL return value. (保留供将来使用.传递除零以外的任何值可能会导致NULL返回值.)
    
        // 1. 全局队列的同步任务
        for (int i = 0; i < 10; i++) {
            dispatch_sync(global_q, ^{ // 同步任务,顺序执行,会在主线程上运行
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
    
        // 2. 全局队列的异步任务
        for (int i = 0; i < 10; i++) {
            dispatch_async(global_q, ^{ // 异步任务,并发执行,会在子线程上运行(可能会新建多个子线程)
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
    }
    
    
    #pragma mark - 主队列(主线程): 保证操作在主线程上执行
    
    - (void)GCD4 { 
         // 每一个应用程序都只有一个主线程.
         // - 为什么需要在主线程上工作呢?原因:在iOS开发中,所有UI的更新工作都必须在主线程上执行.
         // - 主队列属于串行队列类型.
        
        dispatch_queue_t main_q = dispatch_get_main_queue();
    
        // 线程阻塞了!  原因:主线程是有工作的,而且除非将程序kill掉,否则主线程的工作永远不会结束,所以不会执行到 NSLog(@"main_q %@", [NSThread currentThread]); 这一行代码.
        
        // 1. 主队列的同步任务
    //    dispatch_sync(main_q, ^{ // 同步任务,顺序执行
    //        NSLog(@"main_q %@", [NSThread currentThread]); // 进入断点
    //    });
    
        // 2. 主队列的异步任务
        for (int i = 0; i < 10; i++) {
            dispatch_async(main_q, ^{ // 异步任务,在主线程中会依次顺序执行
                NSLog(@"%@ - %d", [NSThread currentThread], i);
            });
        }
     
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
        
    @end
    

    相关文章

      网友评论

          本文标题:iOS开发中的四种GCD

          本文链接:https://www.haomeiwen.com/subject/kwvjjqtx.html