GCD

作者: 纯情扫地僧 | 来源:发表于2017-09-27 14:09 被阅读0次

1、同步串行队列

/**
 不会创建新的线程,队列中的任务遵循FIFO
 */
 - (void)syncSerial{

    NSLog(@">>>>>>>>start<<<<<<<<<<<<");
    dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
        }
    });
    NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}

2、同步并行队列

/**
 不会创建新的线程,队列中的任务遵循FIFO
 */
- (void)syncConCurrent {

    NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
    dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
        }
    });
    NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}

3、异步串行队列

/**
 开启一条新线程
 队列中的任务遵循FIFO执行
 */
- (void)ayncSerial {

    NSLog(@">>>>>>>>start<<<<<<<<<<<<");
    dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
        }
    });

    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
        }
    });
     NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}

4、异步并行队列

/**
 异步并行队列 
 开启多条新线程,任务并发执行
 获取全局队列dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 *  - DISPATCH_QUEUE_PRIORITY_HIGH:
 *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:
 *  - DISPATCH_QUEUE_PRIORITY_LOW:
 *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND:
 第一个参数:队列优先级(2:HIGH  0:DEFAUL -2:LOW INT16_MIN:BACKGROUND:)
 第二个参数:暂时未用到(Reserved for future use)
*/
- (void)ayncConCurrent {

    NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
    //创建方式一:获取全局并发队列
//    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //创建方式二:
    dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}

5、死锁

主线程中创建同步串行队列
- (void)syncLock {

     NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
    dispatch_queue_t queue = dispatch_get_main_queue() ;
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i < 100; i ++) {
            NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
        }
    });
    NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}

主线程中直接调用该方法会导致死锁

[self syncLock]

原因:dispatch_sync会阻塞当前线程,然后把block中的任务放到指定的线程中执行,但此时主线程被阻塞,所以block中的任务始终不能完成,导致死锁现象。
解决方法:在主线程中开启新的线程

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    [self syncLock];
 });
死锁.jpg

相关文章

  • 多线程之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内核(...

  • 7.3 多线程-GCD

    多线程-GCD 多线程-GCD-串行并行 多线程-GCD.png GCD-线程的通讯、延时操作、定时器 GCD-线...

  • iOS 多线程--GCD

    一、GCD基本介绍 1.GCD简介 GCD是Grand Central Dispatch的缩写,GCD是苹果推出的...

  • 自用算法模板(JAVA版)

    一、数论 1)GCD GCD(求最大公约数) QGCD(快速GCD) extGCD(拓展GCD,解决ax + by...

  • GCD介绍

    一、GCD简单介绍 什么是GCD GCD优势 任务和队列 GCD有2个核心概念 GCD的使用就2个步骤 将任务添加...

  • 7.多线程基础(七)GCD加强

    1.GCD串行队列和并发队列 2.GCD延时执行 3.GCD线程组:(的作用) 4.GCD定时器: GCD的实现 ...

网友评论

      本文标题:GCD

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