//总结如下:
//1.同步请求:不会开启新的线程
//1-1.同步请求--串行队列:不开启新线程--按照顺序执行下去
//1-2.同步请求--并发列队:不开启新线程--按照顺序执行下去 //2.异步请求:会开启新的线程
//2-1.异步请求--串行队列:开启一条新线程--按照顺序执行下去
//2-2.异步请求--并发队列:每条请求开启一条新线程--无顺序执行 //3.主线程
//主线程上面的请求都会在主线程上面执行
//3-1.异步请求:在主线程上面不会开启新的线程--按照顺序执行下去
//3-2.同步请求:卡死 当触发同步请求的时候会立即触发这个方法,但是上面的方法还需要往下面走,相互之间卡死了。 - (void)viewDidLoad {
[super viewDidLoad]; [self test8]; }
// <NSThread: 0x7fd2c3c088a0>{number = 1, name = main}
// 2><NSThread: 0x7fd2c3c14280>{number = 2, name = (null)}
// 4><NSThread: 0x7fd2c3c9a6a0>{number = 4, name = (null)}
// 3><NSThread: 0x7fd2c3e0c4b0>{number = 3, name = (null)}
// 1><NSThread: 0x7fd2c3e0d570>{number = 5, name = (null)}
// 5><NSThread: 0x7fd2c3c14280>{number = 2, name = (null)}
//异步队列--异步请求
//会创建新的线程--线程之间的执行时是并发的
-(void)test1{ NSLog(@"%@",[NSThread currentThread]); 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]);
});
} //结果如下
// <NSThread: 0x7fd5bac04910>{number = 1, name = main}
// 1><NSThread: 0x7fd5bac04910>{number = 1, name = main}
// 2><NSThread: 0x7fd5bac04910>{number = 1, name = main}
// 3><NSThread: 0x7fd5bac04910>{number = 1, name = main}
// 4><NSThread: 0x7fd5bac04910>{number = 1, name = main}
// 5><NSThread: 0x7fd5bac04910>{number = 1, name = main}
//异步队列--同步请求
//并没有开启新的线程--还是在主线程上面串行的执行
-(void)test2{ NSLog(@"%@",[NSThread currentThread]); 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]);
});
} //结果如下
// <NSThread: 0x7fed50d07b50>{number = 1, name = main}
// 1><NSThread: 0x7fed50d07b50>{number = 1, name = main}
// 2><NSThread: 0x7fed50d07b50>{number = 1, name = main}
// 3><NSThread: 0x7fed50d07b50>{number = 1, name = main}
// 4><NSThread: 0x7fed50d07b50>{number = 1, name = main}
// 5><NSThread: 0x7fed50d07b50>{number = 1, name = main}
//串行队列--同步请求
//并没有开启新的线程--还是在主线程上面串行的执行
-(void)test3{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
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]);
});
} //结果如下
// <NSThread: 0x7faa8a606430>{number = 1, name = main}
// 1><NSThread: 0x7faa8a61e880>{number = 2, name = (null)}
// 2><NSThread: 0x7faa8a61e880>{number = 2, name = (null)}
// 3><NSThread: 0x7faa8a61e880>{number = 2, name = (null)}
// 4><NSThread: 0x7faa8a61e880>{number = 2, name = (null)}
// 5><NSThread: 0x7faa8a61e880>{number = 2, name = (null)}
//串行队列--异步请求
//开启了一条新的线程--按照顺序执行
-(void)test4{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
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]);
});
} //结果如下
// <NSThread: 0x7fe849c048f0>{number = 1, name = main}
// 1><NSThread: 0x7fe849c048f0>{number = 1, name = main}
// 2><NSThread: 0x7fe849c048f0>{number = 1, name = main}
// 3><NSThread: 0x7fe849c048f0>{number = 1, name = main}
// 4><NSThread: 0x7fe849c048f0>{number = 1, name = main}
// 5><NSThread: 0x7fe849c048f0>{number = 1, name = main}
//主线程--异步请求
//不会开启新的线程,都是在主线程上面执行的
-(void)test5{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_main_queue();
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]);
});
} //结果如下
// <NSThread: 0x7fe881501cc0>{number = 1, name = main}
//主线程--同步请求
//卡主线程
-(void)test6{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_main_queue();
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]);
});
} //结果如下
// <NSThread: 0x7fc70a7023c0>{number = 1, name = main}
// 2><NSThread: 0x7fc70a443ac0>{number = 2, name = (null)}
// 3><NSThread: 0x7fc70a61a740>{number = 4, name = (null)}
// 1><NSThread: 0x7fc70a70ea60>{number = 3, name = (null)}
// done //开启多条线程
//统一完成后 -(void)test7{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1>%@",[NSThread currentThread]);
}); dispatch_group_async(group, queue, ^{
NSLog(@"2>%@",[NSThread currentThread]);
}); dispatch_group_async(group, queue, ^{
NSLog(@"3>%@",[NSThread currentThread]);
}); dispatch_group_notify(group, queue, ^{
NSLog(@"done");
});
}

GCD汇总的更多相关文章

  1. GCD使用汇总

    本文目录 dispatch_queue_t.dispatch_block_t dispatch_sync.dispatch_async dispatch_set_target_queue.dispat ...

  2. iOS 多线程之GCD的使用

    在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...

  3. GCD-两个网络请求同步问题

    在网络请求的时候有时有这种需求 两个接口请求数据,然后我们才能做最后的数据处理.但是因为网络请求是移步的 .我们并不知道什么时候两个请求完成 . 通常面对这样的需求会自然的想到 多线程 啊 .表现真正 ...

  4. GCD的简单用法

    /* 创建一个队列用来执行任务,TA属于系统预定义的并行队列即全局队列,目前系统预定义了四个不同运行优先级的全局队列,我们可以通过dispatch_get_global_queue来获取它们 四种优先 ...

  5. iOS多线程 GCD常见用法

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  6. GCD的使用

    什么是 GCD Grand Central Dispatch (GCD) 是 Apple 开发的一个多核编程的解决方法.该方法在 Mac OS X 10.6 雪豹中首次推出,并随后被引入到了 iOS4 ...

  7. iOS多线程GCD

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  8. iOS学习之GCD

    多线程编程 线程定义:一个CPU执行的CPU命令 列一条无分叉的路径就叫线程. 多线程:执行多个不同的CPU命令 有多条路径. 线程的使用:主线程(又叫作UI线程)主要任务是处理UI事件,显示和刷新U ...

  9. IOS中的多核并发编程GCD

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

随机推荐

  1. c++ wchar_t

    ·C语言相关 对应于char, C语言中也有宽字符内型wchar_t.wchar_t被定义为: typedef unsigned short wchar_t ;显然它是16位的.wchar_t类型的常 ...

  2. 微信小程序直播

    微信小程序直播(转) 通过PC实现推流,然后用小程序进行直播播放,也就是PC->小程序. 小程序支持 小程序的直播能力只针对某些类目开放并且需要申请开通. 支持的类目 社交 直播 教育 在线教育 ...

  3. 一个页面从输入URL到页面加载完成发生了...待细化

    一个页面从输入URL到页面加载完成发生了... 1.查找浏览器缓存 2.寻址:DNS解析 查找该域名对应的IP地址, 如果需要重定向(301),则再次发起请求 3. 进行HTTP协议会话 4.客户端发 ...

  4. XXE(xml外部实体注入漏洞)

    实验内容 介绍XXE漏洞的触发方式和利用方法,简单介绍XXE漏洞的修复. 影响版本: libxml2.8.0版本 漏洞介绍 XXE Injection即XML External Entity Inje ...

  5. 推流协议 支持RTMP协议推流

    Stream Type Stream play domain nameStreaming Domain Name   播流 推流  推流协议 支持RTMP协议推流

  6. IO流入门-第十章-DataInputStream_DataOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.DataOutputStream 数据字节输出流,带着类型写入 可以将内存中的“int i = ...

  7. Java 其他对象的 API

    System 类 (java.lang 包下) 该类中的方法和属性都是静态的. 常见方法 // 1, 获取当前时间的毫秒值 long currentTimeMillis(); // 2, 获取系统的属 ...

  8. NUnit.Framework的使用方法演示

    using NUnit.Framework; namespace CheckExcel { [TestFixture] public class TestExcelHelper { /// <s ...

  9. Python(变量、数据类型)

    常量:python中没有常量,只能通过名字特征来提示例如:全部大写,如 : OLDBOY_AGE=57 一.变量 变量声明变量#!/usr/bin/env python age=18gender1=' ...

  10. Linux基础——硬盘分区、格式化及文件系统的管理

    1. 硬件设备与文件名的对应关系 掌握在Linux系统中,每个设备都被当初一个文件来对待. 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a-d] SCSI硬盘 /dev/sd[a-p ...