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

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

  • 相关阅读:
    gnuplot 让您的数据可视化
    sort
    sed
    AWK
    STA之RC Corner再论
    STA之RC Corner拾遗
    网络编程释疑之:TCP半开连接的处理
    Task 任务内部揭秘
    Task 线程任务
    【转】SQL Server、Oracle、MySQL和Vertica数据库常用函数对比
  • 原文地址:https://www.cnblogs.com/jiahao89/p/5118285.html
Copyright © 2011-2022 走看看