GCD队列

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self syncConcurrent];
}

  • 同步函数+主队列 没有开启新的线程 串行执行任务 会造成线程阻塞 你等我来我等你结果就耗着了
 -(void)syncMain{
    //1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"%s",__func__);
    //2、将任务加进队列
dispatch_sync(queue, ^{
    NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
/*2016-07-27 22:03:18.257 GCD[1474:205778] -[ViewController syncMain]*/
}
  • 异部函数+主队列 只在主线程中执行任务,没有开启新线程 串行执行任务
-(void)asyncMain{
//1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
//2、将任务添加进队列
dispatch_async(queue, ^{
    NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"3----%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:01:12.524 GCD[1461:204697] 1----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 2016-07-27 22:01:12.525 GCD[1461:204697] 2----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 2016-07-27 22:01:12.526 GCD[1461:204697] 3----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 */
}
  • 同步函数+串行队列 开启新的线程 串行执行任务
-(void)syncSerial{
//1、获得串行队列
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL);
//2、将任务添加到队列中
dispatch_sync(queue, ^{
     NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"3----%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:09:31.890 GCD[1509:209789] 1----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 2016-07-27 22:09:31.891 GCD[1509:209789] 2----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 2016-07-27 22:09:31.892 GCD[1509:209789] 3----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 */
}
  • 异部函数 + 串行队列 :会开启新的线程,但是任务是串行的,执行完一个任务,在执行下一个任务

    -(void)asyncSerial{
    
    dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
      NSLog(@"1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"2----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"3----%@",[NSThread currentThread]);
    });
    /*
     2016-07-27 22:11:47.582 GCD[1531:211558] 1----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     2016-07-27 22:11:47.584 GCD[1531:211558] 2----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     2016-07-27 22:11:47.585 GCD[1531:211558] 3----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     */
    }
    
  • 同步函数 + 并行队列 :不会开启新的线程,串行执行任务

-(void)syncConcurrent{
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
    NSLog(@"1-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"3-------%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:20:11.875 GCD[1560:215430] 1-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 2016-07-27 22:20:11.877 GCD[1560:215430] 2-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 2016-07-27 22:20:11.878 GCD[1560:215430] 3-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 */
}
  • 异步函数 + 并行队列:有开启新的线程,并发执行任务

    -(void)asyncConcurrent{
    dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
      NSLog(@"1-------%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"2-------%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"3-------%@",[NSThread currentThread]);
    });
    /*
     2016-07-27 22:17:14.859 GCD[1548:213931] 1-------<NSThread: 0x7faaf170fcd0>{number = 5, name = (null)}
     2016-07-27 22:17:14.860 GCD[1548:213866] 2-------<NSThread: 0x7faaf140b5a0>{number = 4, name = (null)}
     2016-07-27 22:17:14.860 GCD[1548:213860] 3-------<NSThread: 0x7faaf14069e0>{number = 2, name = (null)}
     */
    }
    
并发队列 手动创建的串行队列 主队列
同步 没有开启新线程 串行执行任务 没有开启新线程 串行执行任务 没有开启新线程 串行执行任务
异步 有开启新线程 并发执行任务 有开启新线程 串行执行任务 没有开启新线程 串行执行任务
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. GCD简介 什么是GCD呢?我们先来看看百度百科的解释简单了解下概念 引自百度百科:Grand Centra...
    千寻_544f阅读 3,043评论 0 0
  • 文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步执行并行...
    lusen_b阅读 1,709评论 0 1
  • GCD核心概念 任务 :执行GCD函数block(代码块)中的代码 队列 :用来存放任务的队列,遵循FIFO原则 ...
    bluajack阅读 4,168评论 1 4
  • 1.情绪影响你的精力 情绪,作为精力很重要的一个组成部分,它会影响精力的高低与正负。当一个人没有精力的时候或者低情...
    睡午觉的喵咪阅读 3,325评论 0 2
  • 1)去饱和皮肤效果 对背景人物再复制出两个图层 对中间图层 ctrl+shift+u 进行去饱和操作,并删除该...
    奇奇本子阅读 1,261评论 0 0