iOS 多线程GCD的基本使用
《iOS多线程简介》中提到:GCD中有2个核心概念:1、任务(执行什么操作)2、队列(用来存放任务)
那么多线程GCD的基本使用有哪些呢?
可以分以下多种情况:
1、异步函数 + 并发队列
/**
* 异步函数 + 并发队列:可以同时开启多条线程
*/
- (void)asyncConcurrent
{
// 1.创建一个并发队列
// dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
// label : 相当于队列的名字
// dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_CONCURRENT); // 1.获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); // 2.将任务加入队列
dispatch_async(queue, ^{
for (NSInteger i = ; i<; i++) {
NSLog(@"this is first %@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = ; i<; i++) {
NSLog(@"this is second %@",[NSThread currentThread]);
}
}); dispatch_async(queue, ^{
for (NSInteger i = ; i<; i++) {
NSLog(@"this is third %@",[NSThread currentThread]);
}
}); }

2、同步函数 + 并发队列
/**
* 同步函数 + 并发队列:不会开启新的线程
*/ - (void)syncConcurrent
{
// 1.获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); // 2.将任务加入队列
dispatch_sync(queue, ^{
NSLog(@"this is first %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"this is second %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"this is third %@",[NSThread currentThread]);
}); }

3、异步函数 + 串行队列
/**
* 异步函数 + 串行队列:会开启新的线程,但是任务是串行的,执行完一个任务,再执行下一个任务
*/
- (void)asyncSerial
{
// 1.创建串行队列
dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_SERIAL);
// dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", NULL); // 2.将任务加入队列
dispatch_async(queue, ^{
NSLog(@"this is first %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"this is second %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"this is third %@",[NSThread currentThread]);
});
}

4、同步函数 + 串行队列
/**
* 同步函数 + 串行队列:不会开启新的线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务
*/
- (void)syncSerial
{
// 1.创建串行队列
dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_SERIAL); // 2.将任务加入队列
dispatch_sync(queue, ^{
NSLog(@"this is first %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"this is second %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"this is third %@",[NSThread currentThread]);
});
}

5、异步函数 + 主队列
// * 异步函数 + 主队列:只在主线程中执行任务 - (void)asyncMain
{
// 1.获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.将任务加入队列
dispatch_async(queue, ^{
NSLog(@"this is first %@",[NSThread currentThread]);
NSLog(@"this is second %@",[NSThread currentThread]);
NSLog(@"this is third %@",[NSThread currentThread]);
});
}

6、同步函数 + 主队列
// * 同步函数 + 主队列:
- (void)syncMain
{
NSLog(@"begin"); // 1.获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.将任务加入队列
dispatch_sync(queue, ^{
NSLog(@"this is %@",[NSThread currentThread]); });
NSLog(@"end");
}
造成“相互等待的死锁”
iOS 多线程GCD的基本使用的更多相关文章
- iOS多线程 GCD
iOS多线程 GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main que ...
- iOS 多线程 GCD part3:API
https://www.jianshu.com/p/072111f5889d 2017.03.05 22:54* 字数 1667 阅读 88评论 0喜欢 1 0. 预备知识 GCD对时间的描述有些新奇 ...
- ios多线程-GCD基本用法
ios中多线程有三种,NSTread, NSOperation,GCD 这篇就讲讲GCD的基本用法 平时比较多使用和看到的是: dispatch_async(dispatch_get_global_q ...
- iOS多线程——GCD与NSOperation总结
很长时间以来,我个人(可能还有很多同学),对多线程编程都存在一些误解.一个很明显的表现是,很多人有这样的看法: 新开一个线程,能提高速度,避免阻塞主线程 毕竟多线程嘛,几个线程一起跑任务,速度快,还不 ...
- iOS多线程GCD的使用
1. GCD 简介 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Di ...
- iOS多线程GCD详解
在这之前,一直有个疑问就是:gcd的系统管理多线程的概念,如果你看到gcd管理多线程你肯定也有这样的疑问,就是:并发队列怎么回事,即是队列(先进先出)怎么会并发,本人郁闷了好久,才发现其实cgd管理多 ...
- iOS多线程GCD的简单使用
在iOS开发中,苹果提供了三种多线程技术,分别是: (1)NSThread (2)NSOperation (3)GCD 简单介绍一下GCD的使用. GCD全称 Grand Central Dispat ...
- iOS多线程——GCD篇
什么是GCD GCD是苹果对多线程编程做的一套新的抽象基于C语言层的API,结合Block简化了多线程的操作,使得我们对线程操作能够更加的安全高效. 在GCD出现之前Cocoa框架提供了NSObjec ...
- iOS 多线程GCD简介
一.简介 1.1 GCD (Grand Central Dispatch )是Apple开发的一个多核编程的解决方法. Grand 含义是“伟大的.宏大的”,Central含义“中央的”,Dispat ...
随机推荐
- Utility2:Appropriate Evaluation Policy
UCP收集所有Managed Instance的数据的机制,是通过启用各个Managed Instances上的Collection Set:Utility information(位于Managem ...
- 深入理解javascript作用域系列第五篇——一张图理解执行环境和作用域
× 目录 [1]图示 [2]概念 [3]说明[4]总结 前面的话 对于执行环境(execution context)和作用域(scope)并不容易区分,甚至很多人认为它们就是一回事,只是高程和犀牛书关 ...
- Windows下使用Dev-C++开发基于pthread.h的多线程程序
一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录 我选择的目录是:E:\DEV-C ...
- JavaScript变量声明提前
上周四吃完午饭,leader发了一道JavaScript的题目给我们做,我们Team里面有做前端的,有做后台的,也有做mobile web的,所以大家对题目的理解各自都不一样,然后在QQ讨论组里面进行 ...
- 程序员眼中的 SQL Server-非聚集索引能给我们带来什么?
写在前面 最近在做的一个项目,页面访问的时候很慢(大概几秒钟的样子),然后用日志记录的方式,来排查这个问题,最后发现是 Entity Framework 初始化的一个坑(大概要花 6-7 秒),详见: ...
- java 开发中经常问到得懒汉模式 (单利模式)
//懒汉模式 class Single { public static Single s = null; public Single (){} public static Single getInst ...
- ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码
这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- Centos6 yum安装openldap+phpldapadmin+TLS+双主配置
原文地址:http://54im.com/openldap/centos-6-yum-install-openldap-phpldapadmin-tls-%E5%8F%8C%E4%B8%BB%E9%8 ...
- 8.3 使用Fluent API进行属性映射【Code-First系列】
现在,我打算学习,怎么用Fluent API来配置领域类中的属性. using System; using System.Collections.Generic; using System.Linq; ...