zoukankan      html  css  js  c++  java
  • 刀哥多线程同步任务作用gcd-07-sync_task

    同步任务的作用

    同步任务,可以让其他异步执行的任务,依赖某一个同步任务

    例如:在用户登录之后,再异步下载文件!

    - (void)gcdDemo1 {
        dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", DISPATCH_QUEUE_CONCURRENT);
    
        dispatch_sync(queue, ^{
            NSLog(@"登录 %@", [NSThread currentThread]);
        });
    
        dispatch_async(queue, ^{
            NSLog(@"下载 A %@", [NSThread currentThread]);
        });
    
        dispatch_async(queue, ^{
            NSLog(@"下载 B %@", [NSThread currentThread]);
        });
    }
    • 代码改造,让登录也在异步执行
    - (void)gcdDemo2 {
        dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", DISPATCH_QUEUE_CONCURRENT);
    
        void (^task)() = ^{
            dispatch_sync(queue, ^{
                NSLog(@"登录 %@", [NSThread currentThread]);
            });
    
            dispatch_async(queue, ^{
                NSLog(@"下载 A %@", [NSThread currentThread]);
            });
    
            dispatch_async(queue, ^{
                NSLog(@"下载 B %@", [NSThread currentThread]);
            });
        };
    
        dispatch_async(queue, task);
    }
    • 主队列调度同步队列不死锁
    - (void)gcdDemo3 {
    
        dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", DISPATCH_QUEUE_CONCURRENT);
    
        void (^task)() = ^ {
            dispatch_sync(dispatch_get_main_queue(), ^{
                NSLog(@"死?");
            });
        };
    
        dispatch_async(queue, task);
    }

    主队列在主线程空闲时才会调度队列中的任务在主线程执行

  • 相关阅读:
    HDU_3496_(二维费用背包)
    HDU_3732_(多重背包)
    HDU_2079_(01背包)(dfs)
    HDU_2844_(多重背包)
    Codeforces_766_D_(并查集)
    HDU_3591_(多重背包+完全背包)
    struts2标签
    ongl 表达式
    result 相关
    struts2页面输出错误信息
  • 原文地址:https://www.cnblogs.com/jiahao89/p/5118285.html
Copyright © 2011-2022 走看看