zoukankan      html  css  js  c++  java
  • GCD的基本使用

    GCD全称Grand Central Dispatch,是Apple提供的一套底层API,提供了一种新的方法来进行并发程序编写,它的API包含在libdispatch库中.

    觉得需要理解GCD中的三个要点:

    1.同步异步(sync,async)

    sync表示同步,不会开启新线程,任务是在当前线程中执行; async表示异步,具备开启新线程的能力,具体是否开启新线程需要看队列,比如:全局队列,串型队列会开启新线程,主队列就不会开启

    2.队列

    串型队列:

    dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL)

     

    并发队列:

    dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);

    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ,其中第一个参数目前有四种选择:

    #define DISPATCH_QUEUE_PRIORITY_HIGH 2 优先级搞
    #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认级别 一般开发采用
    #define DISPATCH_QUEUE_PRIORITY_LOW (-2) 优先级低
    #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 优先级最低

     

    主队列:

    dispatch_get_main_queue()

     

    3.任务

    GCD中执行的任务,需要执行的代码写入此处就OK,GCD任务一般情况下都是在block中.

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    //    [self asyncConcurrent];
        [self asyncSerial];
    //    [self asyncMain];
        
    //    [self syncMain];
    //    [self syncSerial];
    //    [self syncConcurrent];
        
    }
    
    - (void)syncMain{
        // 线程阻塞
        dispatch_queue_t queue = dispatch_get_main_queue();
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
    - (void)syncSerial{
        // 在当前线程中顺序执行任务
        dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
    - (void)syncConcurrent{
        // 在当前线程中顺序执行任务
        dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_sync(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
    
    
    - (void)asyncMain{
        // Main线程中异步执行任务
        dispatch_queue_t queue = dispatch_get_main_queue();
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
    - (void)asyncSerial{
        // 开启一条新线程顺序执行任务
        dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
    - (void)asyncConcurrent{
        // 开启多条新线程顺序执行任务
        dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        dispatch_async(queue, ^{
            for (NSInteger i = 0; i<10; i++) {
                NSLog(@"i=%ld %@",i,[NSThread currentThread]);
            }
        });
        
        NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
    }
    
  • 相关阅读:
    Powered by .NET Core 进展0815:第5次发布尝试(Windows部署)团队
    峰回路转:去掉 DbContextPool 后 Windows 上的 .NET Core 版博客表现出色团队
    做梦也没有想到:Windows 上的 .NET Core 版博客系统表现更糟糕团队
    全网最详细的zkfc启动以后,几秒钟以后自动关闭问题的解决办法(图文详解)
    全网最详细的HBase启动以后,HMaster进程启动了,几秒钟以后自动关闭问题的解决办法(图文详解)
    全网最详细的启动或格式化zkfc时出现java.net.NoRouteToHostException: No route to host ... Will not attempt to authenticate using SASL (unknown error)错误的解决办法(图文详解)
    全网最详细的HA集群的主节点之间的双active,双standby,active和standby之间切换的解决办法(图文详解)
    全网最详细的启动zkfc进程时,出现INFO zookeeper.ClientCnxn: Opening socket connection to server***/192.168.80.151:2181. Will not attempt to authenticate using SASL (unknown error)解决办法(图文详解)
    全网最详细的再次或多次格式化导致namenode的ClusterID和datanode的ClusterID之间不一致的问题解决办法(图文详解)
    执行bin/hdfs haadmin -transitionToActive nn1时出现,Automatic failover is enabled for NameNode at bigdata-pro02.kfk.com/192.168.80.152:8020 Refusing to manually manage HA state的解决办法(图文详解)
  • 原文地址:https://www.cnblogs.com/HJiang/p/7448213.html
Copyright © 2011-2022 走看看