GCD 同步异步的串行和并发问题

    //1.主对列:(串行队列)
    
    dispatch_queue_t mainQueue=dispatch_get_main_queue();
    
    NSLog(@"%@",[NSThread currentThread]);
    
    
    
    
    //2.全局并行队列
    
    dispatch_queue_t concu=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    
    
    
    
    
    //3.创建串行队列
    
    dispatch_queue_t queueSerial=dispatch_queue_create("jrqueue1",DISPATCH_QUEUE_SERIAL);
    
    
    
    
    
    //4.创建并行队列
    
    dispatch_queue_t queueConcu=dispatch_queue_create("jrqueue2",DISPATCH_QUEUE_CONCURRENT);
    
    
    
    
    

    //同步执行+串行队列

//主队列:卡死(死锁)

//自定义:顺序执行 走主线程

    
    /*
     
     dispatch_sync(queueSerial, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步串行队列1-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_sync(queueSerial, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步串行队列2-----%@",[NSThread currentThread]);
     
     });
     
    
    */
    
    
    //同步执行+并行队列  走主线程

//主队列:顺序执行

//自定义:顺序执行  

    
    /*
     dispatch_sync(queueConcu, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步并行队列1-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_sync(queueConcu, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步并行队列2-----%@",[NSThread currentThread]);
     
     });
     
    
    */
    
    
    //异步执行+串行队列-----开启一个子线程,且顺序执行
//主队列:顺序执行(走主线程)   
//自定义:顺序执行:(开辟子线程,子队列共用一个线程)   
    
     dispatch_async(queueSerial, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步串行队列1-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_async(queueSerial, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步串行队列2-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_async(queueSerial, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步串行队列3-----%@",[NSThread currentThread]);
     
     });
     
    

    
    
    //异步执行+并行队列----开启多个线程,且并发执行(无序)

 

//主队列: 无序执行(开辟子线程,每一个任务单独开辟一个线程)

//自定义:无序执行(开辟子线程,每一个任务单独开辟一个线程)

//相同:当主线程走到子线程是,子线程会同时执行 


/*

    
     dispatch_async(concu, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步并行队列1-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_async(concu, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步并行队列2-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_async(concu, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"异步并行队列3-----%@",[NSThread currentThread]);
     
     });
     
    
    */
    
    
    //主对列+同步执行-----死锁(将以下两个添加到主队列,等待前面的执行完成(loadView。loadData之类的),但是当执行到这一步时,形成死循环)
    
    
     /*
     dispatch_sync(mainQueue, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步主队列1-----%@",[NSThread currentThread]);
     
     });
     
     
     
     dispatch_sync(mainQueue, ^{
     
     [NSThread sleepForTimeInterval:2];
     
     NSLog(@"同步主队列2-----%@",[NSThread currentThread]);
     
     });
     
    */
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值