美文网首页iOS Developer工具癖代码改变世界
GCD:串行/并行/主队列下,同步/异步的执行方式

GCD:串行/并行/主队列下,同步/异步的执行方式

作者: Ro_bber | 来源:发表于2017-06-18 09:30 被阅读62次

喜欢我的可以关注收藏我的个人博客:RobberJJ

GCD中获取各种类型的队列:

//获取串行的队列  
dispatch_queue_t singalQueue = dispatch_queue_create("single",DISPATCH_QUEUE_SERIAL);
//获取并发执行的队列
dispatch_queue_t concrtQueue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
//获取主队列
dispatch_queue_t mainQueue =  dispatch_get_main_queue();
//获取全局的队列(并发的)
dispatch_queue_t gobalqueue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

<h6>串行队列异步执行任务</h6>

  1. 异步具有创建新线程的能力,会开辟新线程去执行任务;
  1. 按照串行的方式去执行任务。

如下调用方式可参考:
- (void)singalAsynQueue{
//创建串行队列
dispatch_queue_t singalQueue = dispatch_queue_create("singal", DISPATCH_QUEUE_SERIAL);
//在singalQueue中异步执行任务(该方法实现在本文后续中)
[self asynWithQueue: singalQueue];
}

<h6>串行队列同步执行任务</h6>

  1. 同步不具有创建新的线程的能力, 不会开辟新的线程去执行任务,会在当前的程序的主线程中去执行任务;
  1. 按照串行的方式去执行任务。

如下调用方式可参考:
- (void)singalSynQueue{
//创建串行队列
dispatch_queue_t singalQueue = dispatch_queue_create("singal", DISPATCH_QUEUE_SERIAL);
//在singalQueue中同步执行任务(该方法实现在本文后续中)
[self synWithQueue: singalQueue];
}

<h6>并发队列异步执行任务(常用)</h6>

  1. 异步具有创建新的线程的能力,会开辟新的线程去执行任务,不会在当前的程序的主线程中去执行任务;
  1. 按照并发的方式去执行任务。

如下调用方式可参考:
- (void)concrtAsynQueue{
//创建并发执行的队列
// dispatch_queue_t concrtQueue = dispatch_queue_create("concrtQueue", DISPATCH_QUEUE_CONCURRENT);
//获取全局的队列
dispatch_queue_t concrtQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//在concrtQueue中异步执行任务(该方法实现在本文后续中)
[self asynWithQueue:concrtQueue];
}

<h6>并发队列同步执行任务</h6>

  1. 同步不具有创建新的线程的能力, 不会开辟新的线程去执行任务,会在当前的程序的主线程中去执行任务;
  1. 按照同步的方式去执行任务。

如下调用方式可参考:
- (void)concrtSynQueue{
//创建并发执行的队列
// dispatch_queue_t concrtQueue = dispatch_queue_create("concrtQueue", DISPATCH_QUEUE_CONCURRENT);
//获取全局的队列
dispatch_queue_t concrtQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//在concrtQueue中同步执行任务(该方法实现在本文后续中)
[self synWithQueue:concrtQueue];
}

<h6>主队列的同步(会造成程序的死锁)</h6>

如下:
- (void)mainSynQueue{
//获取主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//在mainQueue中同步执行任务(该方法实现在本文后续中)
[self synWithQueue:mainQueue];
}

<h6>主队列的异步(在主线程中顺序执行)</h6>

新添加到主队列中的任务会放到队列的最尾部,等到当前主线程中的任务结束之后然后再从队列的头部取出依次执行(FIFO)先进先出。

如下调用方式可参考:
- (void)mainAsynQueue{
//获取主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//在mainQueue中异步执行任务(该方法实现在本文后续中)
[self asynWithQueue:mainQueue];
}

<h6>异步方法的实现</h6>

- (void)asynWithQueue:(dispatch_queue_t)queue{
    NSLog(@"%@",[NSThread currentThread]);
    
    dispatch_async(queue, ^{
        NSLog(@"----1----%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"----2----%@",[NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----3----%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"----4----%@",[NSThread currentThread]);
    });
    
    NSLog(@"--------end------------");
}

<h6>同步方法的实现</h6>

- (void)synWithQueue:(dispatch_queue_t)queue{
    NSLog(@"%@",[NSThread currentThread]);
    
    dispatch_sync(queue, ^{
        NSLog(@"----1----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"----2----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"----3----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"----4----%@",[NSThread currentThread]);
    });
    
    NSLog(@"--------end------------");
}

相关文章

  • 关于多线程GCD 串行/并行、同步/异步

    一、GCD串行/并行队列创建 串行队列: 并行队列: 二、GCD串行/并行队列同步/异步执行 执行内容1: 执行结...

  • IOS多线程总结

    目录 简述 NSThread GCD操作与队列异步操作并行队列同步操作并行队列同步操作串行队列异步操作串行队列队列...

  • iOS GCD随记(一)任务/队列组合

    1.同步执行+串行队列2.同步执行+并行队列3.异步执行+串行队列4.异步执行+并行队列5.同步执行+主队列6.异...

  • GCD基础总结一

    上代码~ 同步串行队列 同步并行队列 异步串行队列 异步并行队列 主队列同步 会卡住 主队列异步

  • ios线程概念运用

    GCD 整个使用的格式为: 先确定要创建的队列: (串行 并行) 队列中该线程是同步还是异步执行线程() 执行...

  • gcd多线程任务与队列组合分析

    关于gcd中串行队列并行队列,以及同步任务和异步任务的花式嵌套,分析执行结果 多线程调试常用代码: gcd的任务 ...

  • 多线程

    1、同步、异步、串行、并行、全局队列、主队列2、Thread、NSOperation、GCD3、锁

  • GCD

    1、同步串行队列 2、同步并行队列 3、异步串行队列 4、异步并行队列 5、死锁 主线程中创建同步串行队列 主线程...

  • iOS - 多线程(二) GCD讲解

    目录: 1.GCD简介2.串行队列 + 同步执行3.串行队列 + 异步执行4.并发队列 + 同步执行5.并发队列 ...

  • CGD知识小结

    gcd 分为并行,串行两种方式,任务的执行可分为,同步,异步执行方式。 dispatch_queue_tqueue...

网友评论

    本文标题:GCD:串行/并行/主队列下,同步/异步的执行方式

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