美文网首页
10.7 GCD 串行列队, 并行列队

10.7 GCD 串行列队, 并行列队

作者: jayck | 来源:发表于2016-10-07 19:09 被阅读36次

    GCD

    主要学习的知识点有:串行列队,并行列队,死锁,group,栅栏

    ·串行列队:A执行完,执行B,B执行完再执行C 排队执行 SERIAL
    ·并行列队:无序执行 CONCURRENT

    ·死锁没事少用同步方法,很容易造成主线程死锁

    ·栅栏:AB可以无序执行,CD也可以无序执行,但是我们要AB执行完再执行CD,这样我们就不好设置依赖关系了
    ·栅栏就可以挡在AB和CD的中间,等AB执行完了再执行CD

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
    #if 0
    
    //    主线程
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    //    子线程  异步执行
        dispatch_queue_t subQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        dispatch_async(subQueue, ^{
            
            NSLog(@"task");
        });
        
        NSLog(@"hello");
    
    #endif
        
        
    #if 0
    
        //异步执行
        dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
        for (NSInteger i = 0; i<100; i++) {
            
            dispatch_async(globalQueue, ^{
                
               
                NSLog(@"i = %ld",i);
    
            });
        }
    
    #endif
        
        //自己创建一个新的线程
    
        dispatch_queue_t customQueue = dispatch_queue_create("apple.queue",DISPATCH_QUEUE_CONCURRENT);
       
    #if 0
    
        dispatch_sync(customQueue, ^{
            
            NSLog(@"task");
        });
    
        NSLog(@"hello");
    
    #endif
    
       
    #if 0
    
        // 串联列队 有序执行打印 i = 0, i = 1, i = 2....    DISPATCH_QUEUE_SERIAL
    
        customQueue = dispatch_queue_create("apple.queue2", DISPATCH_QUEUE_SERIAL);
        
        customQueue = dispatch_get_main_queue();
        
        for (NSInteger i = 0; i<100; i++) {
        
            dispatch_async(customQueue, ^{
                
                NSLog(@"i = %ld",i);
            });
        }
        
        NSLog(@"hello world");
    
    #endif
        
    #if 0
        
        NSLog(@"hello world");
        //典型的死锁    dispatch等待main打印"welcome",而打印这个方法是在主线程,造成死锁
        //主线程执行dispatch,dispatch又要用到主线程,进入一个死循环中,死锁
        dispatch_sync(dispatch_get_main_queue(), ^{
    
            NSLog(@"welcome");
            
        });
    
        NSLog(@"======");
    #endif 
       
    #if 0
    
        //并联列队     无序执行,我们并不知道哪个先执行完                DISPATCH_QUEUE_CONCURRENT
        dispatch_queue_t queue = dispatch_queue_create("apple.queu3", DISPATCH_QUEUE_CONCURRENT);
    
       dispatch_async(queue, ^{
          
           NSLog(@"1");
       });
       
        dispatch_async(queue, ^{
            
            NSLog(@"2");
        });
    
        dispatch_async(queue, ^{
            
            NSLog(@"3");
        });
    
        NSLog(@"done");
        
    #endif
        
    #if 0
        
        dispatch_group_t group = dispatch_group_create();
    
        dispatch_queue_t queue = dispatch_queue_create("apple.queu3", DISPATCH_QUEUE_CONCURRENT);
    
        dispatch_group_async(group, queue, ^{
            
            NSLog(@"1");
        });
        
        //group_notify不能放在最上面,group里为空时就会执行,放在group最上面的话就为空,其他group_async方法还没有调用它就会先执行
        //所有异步方法执行完后,打印"done"
        dispatch_group_notify(group, queue, ^{
           
            NSLog(@"done");
        });
    
        //异步方法中又有异步方法,比如网络请求,group有可能不知道什么时候才执行notify
        dispatch_group_async(group, queue, ^{
            
            //所以我们设置一个标记,在开始这个异步方法前enter
            dispatch_group_enter(group);
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                NSLog(@"2");
                //执行完后leave
                dispatch_group_leave(group);
            });
        });
    
        dispatch_group_async(group, queue, ^{
            
            NSLog(@"3");
            
        });
    
        dispatch_group_async(group, queue, ^{
            
            NSLog(@"4");
            
        });
    
        dispatch_group_async(group, queue, ^{
            
            NSLog(@"5");
            
        });
    
    #endif
       
        dispatch_queue_t queue = dispatch_queue_create("apple.queu3",DISPATCH_QUEUE_CONCURRENT);
    
    #if 0
    
        //一个异步方法,比如说网络下载
        dispatch_async(queue, ^{
    
            //下载data...
            NSData *data;//....
            
            //当我们要显示时一定要在主线程里面执行
            dispatch_async(dispatch_get_main_queue(), ^{
                //主线程中显示图片
                UIImage *image = [UIImage imageWithData:data];
                
            });
        });
    
    #endif
        
    #if 0
        //1、2可以无序执行,3、4也可以无序执行,但是我们要1、2执行完再执行3、4.  这样我们就不好设置依赖关系了
    
        dispatch_async(queue, ^{
            
            NSLog(@"1");
        });   
        
        dispatch_async(queue, ^{
            
            NSLog(@"2");
        });
    
        //栅栏就可以挡在1、2和3、4的中间,等1、2执行完了再执行3、4
    
        dispatch_barrier_async(queue, ^{
            
            NSLog(@"barrier");
        });
        
        dispatch_async(queue, ^{
            
            NSLog(@"3");
        });
    
        dispatch_async(queue, ^{
            
            NSLog(@"4");
        });
    
    #endif
    
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:10.7 GCD 串行列队, 并行列队

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