美文网首页
GCD排列组合

GCD排列组合

作者: 云溪_Cloud | 来源:发表于2016-05-05 15:13 被阅读105次
/**
 *  同步函数 + 主队列 = 卡住
 */
- (void)syncMain {

    NSLog(@"syncMain-------begin");

    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"1----%@", [NSThread currentThread]);
    });
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"2----%@", [NSThread currentThread]);
    });
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"3----%@", [NSThread currentThread]);
    });

    NSLog(@"syncMain-------end");
}

/**
 *  异步函数 + 主队列 = 不开线程
 *  主队列优先级高于异步函数,所以即使是异步函数,同样不创建线程
 */
- (void)asyncMain {
    dispatch_async(dispatch_get_main_queue(), ^{
         NSLog(@"1----%@", [NSThread currentThread]);
    });
    dispatch_async(dispatch_get_main_queue(), ^{
         NSLog(@"2----%@", [NSThread currentThread]);
    });
    dispatch_async(dispatch_get_main_queue(), ^{
         NSLog(@"3----%@", [NSThread currentThread]);
    });
}

/**
 *  异步函数 + 并发队列 = 开启多条线程,由于并发,会开启多条线程
 */
- (void)asyncConcurrent {
    // 全局队列都是并发队列
    dispatch_queue_t queue =                dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 10; i++) {
            NSLog(@"1----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 10; i++) {
            NSLog(@"2----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 10; i++) {
            NSLog(@"3----%@", [NSThread currentThread]);
        }
     });
}

/**
 *  异步函数 + 串行队列 = 可以开启条线程,但是只会开一条
 */
- (void)asyncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.guoqi.queue", NULL);

    dispatch_async(queue, ^{
        NSLog(@"1----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"2----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"3----%@", [NSThread currentThread]);
    });
}

/**
 *  同步函数 + 串行队列 = 不开线程,顺序执行
 */
- (void)syncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.guoqi.queue", NULL);

    dispatch_sync(queue, ^{
        NSLog(@"1----%@", [NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@"2----%@", [NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@"3----%@", [NSThread currentThread]);
    });
}

/**
 *  同步函数 + 并发队列 = 不开线程,顺序执行
 */
- (void)syncConcurrent {
    dispatch_queue_t queue =     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_sync(queue, ^{
        NSLog(@"1----%@", [NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@"2----%@", [NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@"3----%@", [NSThread currentThread]);
    });
}

相关文章

  • GCD排列组合

  • GCD排列组合

  • ios知识点(9)多线程

    iOS多线程GCD详解 使用GCD iOS多线程中,队列和执行的排列组合结果分析 存在一点小瑕疵,如果同步(syn...

  • iOS多线程排列组合(GCD)

    DEMO地址 1. 串行队列,同步执行 示例代码: 运行结果: 结束可以看到是在主线程顺序执行的 2. 串行队列,...

  • iOS串行队列、并行队列进行同步或者异步任务解析

    IOS中GCD的队列分为串行队列和并行队列,任务分为同步任务和异步任务,他们的排列组合有四种情况,下面分析这四种情...

  • 多线程之GCD

    GCD介绍 1、GCD简介 2、GCD任务和队列 3、GCD 的基本使用 4、GCD 线程间的通信 5、GCD 的...

  • 扩展GCD(求逆元,解同余方程等等)

    首先要知道gcd函数的基本性质:gcd(a,b)=gcd(b,a)=gcd(|a|,|b|)=gcd(b,a%b)...

  • iOS - GCD

    目录 GCD简介 GCD核心概念 GCD队列的使用 GCD的常见面试题 GCD简介 Grand Central D...

  • iOS-多线程:GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(6种不同组合区别) GCD 线程间的通信...

  • 浅析GCD

    GCD目录: 1. GCD简介 为什么要用GCD呢? GCD可用于多核的并行运算GCD会自动利用更多的CPU内核(...

网友评论

      本文标题:GCD排列组合

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