/**

 *  async --
并发队列

 * 会创建线程。一般同一时候开多条

 * 并发运行任务

 */

<span style="font-size:14px;">- (void)asyncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 加入 全局队列 中去 异步 运行
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]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
</span>
<span style="font-size:14px;">-----下载图片4---<NSThread: 0x7fbd52c9b390>{number = 5, name = (null)}
-----下载图片3---<NSThread: 0x7fbd52f00230>{number = 4, name = (null)}
-----下载图片2---<NSThread: 0x7fbd52e3eed0>{number = 3, name = (null)}
-----下载图片5---<NSThread: 0x7fbd52e2eb40>{number = 6, name = (null)}
-----下载图片1---<NSThread: 0x7fbd52c06010>{number = 2, name = (null)}</span>

/**

 *  async --
串行队列

 * 会创建线程, 一般仅仅开1条线程

 * 串行运行任务(一个任务运行完成后再运行下一个任务)

 */

- (void)asyncSerialQueue
{
// 1.创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 2.将任务加入到串行队列中 异步 运行
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]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
}); }
-----下载图片1---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片2---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片3---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片4---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片5---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}

/**

 *  async --
主队列

 */

- (void)asyncMainQueue
{
// 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]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7faaca905000>{number = 1, name = main}

/**

 *  sync --
主队列(会卡死)

 */

- (void)syncMainQueue
{
NSLog(@"syncMainQueue----begin--"); // 1.主队列(加入到主队列中的任务,都会自己主动放到主线程中去运行)
dispatch_queue_t queue = dispatch_get_main_queue(); // 2.加入 任务 到主队列中 异步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
}); NSLog(@"syncMainQueue----end--");
}
syncMainQueue----begin--

/**

 *  sync --
并发队列

 * 不会创建线程

 * 串行运行任务(一个任务运行完成后再运行下一个任务)

 * 并发队列失去了并发的功能

 */

- (void)syncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 加入到 全局并发队列 中 同步 运行
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]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}

/**

 *  sync --
串行队列

 * 不会创建线程

 * 串行运行任务(一个任务运行完成后再运行下一个任务)

 */

- (void)syncSerialQueue
{
// 创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 将 任务 加入到 串行队列 中 同步 运行
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]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fdb40f05020>{number = 1, name = main}

iOS GCD不同场景的使用比較的更多相关文章

  1. iOS 开发之 GCD 不同场景使用

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  2. iOS GCD之dispatch_semaphore(信号量)

    前言 最近在看AFNetworking3.0源码时,注意到在 AFURLSessionManager.m 里面的 tasksForKeyPath: 方法 (L681),dispatch_semapho ...

  3. iOS GCD基础篇 - 同步、异步,并发、并行的理解

    1.关于GCD - GCD全称是Grand Central Dispatch  - GCD是苹果公司为多核的并行运算提出的解决方案  - GCD会自动利用更多的CPU内核(比如双核.四核)  - GC ...

  4. IOS GCD 使用 (二)

     上一节,主要介绍了GCD的基本的概念,这节将用代码深入详细介绍GCD的使用. 一  使用介绍    GCD的使用主要分为三步:创建代码块;选择或创建合适的分发队列;(同步.异步方式)向分发队列提交任 ...

  5. iOS GCD 编程小结

    一.简单介绍 1.GCD简介? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD优势 GCD是苹果公司为多核的并行运算提出的 ...

  6. iOS GCD NSOperation NSThread等多线程各种举例详解(拷贝)

    2年多的iOS之路匆匆而过,期间也拜读来不少大神的博客,近来突然为自己一直做伸手党感到羞耻,是时候回馈社会.回想当年自己还是小白的时候,照着一些iOS多线程教程学,也只是照抄,只知其然.不知其所以然. ...

  7. iOS GCD中的dispatch_group

    假如有一组任务,A,B,C,D,其中ABC是可以并行的,D是必须在ABC任务完成后再执行的. (举个场景,比如吃饭前必须先做菜.做饭和买饮料,然后才能开吃) 1.关于ABC的并行: 采用多线程的方式就 ...

  8. ios - GCD简单小结

    首先GCD两个名词: 队列 同步异步. 队列: 任务放到队列,队列中的任务执行方式取决于执行队列中任务的方式---同步异步. 串行队列: 任务顺序执行,可以叫阻塞队列.只有前面任务完成才执行后面的. ...

  9. iOS GCD 与 NSOperationQueue

    NSOperationQueue ios NSOperation vs. GCD StackOverflow: NSOperation vs. Grand Central Dispatch Blog: ...

随机推荐

  1. 推荐一个国内的免费公共静态cdn

    https://cdn.baomitu.com/ 更新速度比bootcdn要快,收录的库也很齐全.

  2. 在CentOS 7上安装Node.js

    一.安装1.进入官网下载最新版本https://nodejs.org/en/ 选择下载后上传或直接使用wget下载 wget https://nodejs.org/dist/v8.11.2/node- ...

  3. Maximum Product of Word Lengths -- LeetCode

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  4. apache 单独生成模块

    apache 单独生成模块 一般这种模块都是后期自己去生成的,比如一般在安装apache时都会--enable-so  ,允许动态加载模块. 这个模块你可以这样去生成. 1.下载一个与当前使用的apa ...

  5. 监控目前所有连接SQL SERVER的用户信息

    原文:监控目前所有连接SQL SERVER的用户信息 if object_id('p_getlinkinfo','P')is not null drop proc p_getlinkinfo go c ...

  6. ASIHTTPRequest框架使用总结系列之阿堂教程2(同步请求)

    从本篇开始,阿堂结合一些具体代码来说明.在ASIHTTPRequest框架中,与http请求相关的类有ASIHTTPRequest  和 ASIFormDataRequest,其中最常用的是 ASIH ...

  7. python 时间 相关

    http://www.jb51.net/article/47957.htm 不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部 ...

  8. Makefile中的“-I”(大写i),“-L”(大写l),“-l”(小写l)

    用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数, “-I”(大写i):表示包含头文件: “-L”(大写l):表示库文件目录: “-l”(小写l):表示链 ...

  9. ASIHttpRequest请求时的默认编码

    在ASIHttpRequest.m文件 中的 - (id)initWithURL:(NSURL *)newURL方法中找到 [self setDefaultResponseEncoding:NSISO ...

  10. 【Hive】Hive 安装&使用基础

    2 安装 2.1 参考 2.1.1 下载 2.1.1.1 https://mirrors.tuna.tsinghua.edu.cn/apache/hive/stable-2/ 2.1.2 安装指导 2 ...