七、GCD与多线程

2018-01-31  本文已影响7人  faterman

队列与任务的组合

在多线程开发中我们经常会遇到这些概念:并发队列、串行队列、同步任务、异步任务。我们将这四个概念进行组合会有四种结果:串行队列+同步任务、串行队列+异步任务、并发队列+同步任务、并发队列+异步任务。我们对这四种结果进行解释:

  1. 串行队列+同步任务:不会开启新的线程,任务逐步完成。

  2. 串行队列+异步任务:开启新的线程,任务逐步完成。

  3. 并发队列+同步任务:不会开启新的线程,任务逐步完成。

  4. 并发队列+异步任务:开启新的线程,任务同步完成。

我们如果要让任务在新的线程中完成,应该使用异步线程。为了提高效率,我们还应该将任务放在并发队列中。因此在开发中使用最多的是并发队列+异步任务。看代码:

// 串行队列+同步任务:不会开启新的线程,任务逐步完成

- (void)serialSyn{
    
    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"1---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"2---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"3---%@", [NSThread currentThread]);
            
        }
        
    });
    
}

// 串行队列+异步任务 开启新的线程,任务逐步完成

- (void)serialAsyn{
    
    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"1---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"2---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"3---%@", [NSThread currentThread]);
            
        }
        
    });
    
}

// 并发队列+同步任务 不会开启新的线程,任务逐步完成。

- (void)concurrenSyn{
    
    dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"1---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"2---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_sync(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"3---%@", [NSThread currentThread]);
            
        }
        
    });
    
    
    
}

// 并发队列+异步任务 开启新的线程,任务同步完成。

- (void)concurrentAsyn{
    
    dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"1---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"2---%@", [NSThread currentThread]);
            
        }
        
    });
    
    dispatch_async(queue, ^{
        
        for (int i =0; i <3; i ++) {
            
            NSLog(@"3---%@", [NSThread currentThread]);
            
        }
        
    });
    
}
image.png

一个问题引发的论述

下面代码仅打印1,并且会造成线程死锁。

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"===========1");

    dispatch_sync(dispatch_get_main_queue(), ^{

       NSLog(@"===========2");

    });

    NSLog(@"===========3");
}

问题及概念解释

上一篇 下一篇

猜你喜欢

热点阅读