当前位置:网站首页>串行队列的使用
串行队列的使用
2022-07-20 15:43:00 【Hanyang Li】
1. 声明成员变量
/**
GCD 中提供了全局队列
但是,在第三方框架中,为了获取更大的灵活性,通常会自己创建队列
*/
@interface ViewController (){
// 串行队列成员变量
dispatch_queue_t _queue;
}
@end
2. 初始化创建串行队列
_queue = dispatch_queue_create("com.me.queue", DISPATCH_QUEUE_SERIAL);
3. 测试
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo1];
}
///串行队列前套使用 异步正常,同步会挂掉,相互等待
-(void) demo1 {
//串行队列,同步执行不能嵌套使用
dispatch_sync(_queue, ^{
NSLog(@"AAAAAA");
dispatch_sync(_queue, ^{
NSLog(@"BBBBB");
});
});
}
///串行队列的演示
-(void) demo{
for (int i = 0; i<10; i++) {
//耗时的操作需要异步执行
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//数据库操作又需要保证同一时间,只能有一个操作
//同步执行 - 串行线程没有开线程的能力
//同步,能够做到,即使在异步执行所有的数据库操作任务,仍然能够保证顺序
dispatch_sync(_queue, ^{
NSLog(@"%@ %d",NSThread.currentThread, i);
});
});
}
}
边栏推荐
- NVIDIA Inspector 详细使用说明书
- 4.浸泡测试
- The k-th smallest array sum in an ordered matrix
- Prometheus Operator 配置报警
- The seemingly simple input box input is abnormally stuck. Remember a troubleshooting idea for daily performance problems
- Introduction and learning of Jason
- 50个名额限量开放|带着OceanBase年度发布会的消息走来了!
- Pandoc installation, use and quick start
- MySQL on delete cascade [tutorial]
- 【STM32】中断(NVIC)
猜你喜欢
【VS2019】打印汉字乱码的问题
Chongqing Jiaotong University 2021 programming method and online practice examination question solution
Machine learning - detailed derivation of support vector machine theory (including explanation of examples) (I)
【VS2019】创建C工程
Machine learning - detailed derivation of support vector machine theory (including explanation of examples) (III)
CSDN meetup review - from data lake to index middle stage, improve data analysis ROI
不懂点儿统计学,《星球大战》白看了
Semantic segmentation-rethinking bisenet for real time semantic segmentation-2-miou calculation
云开发寝适闹钟微信小程序源码
记 - flink sql upset-kafka sink 数据乱序排查与解决思路【业务数据任意修改】
随机推荐
C语言程序环境和预处理
【STM32】中断(NVIC)
July training (day 20) - binary search tree
敏捷BI到底与传统BI有何不同?
李宏毅《机器学习》丨4. Deep Learning(深度学习)
论文速读:FAIR 最新 ViT 模型 改进多尺度 ViT --- Improved Multiscale Vision Transformers
2022 Niuke summer school first adji
Error reporting after matlab 2021b installation activation: solution to license manager error -103
华为云:一切皆服务,共建全场景智慧金融
50个名额限量开放|带着OceanBase年度发布会的消息走来了!
在VB6 处理pdf 和jpg文件
Relation et différence entre la cartographie des fichiers (mmap) et sendfile et zéro copie
Notes - check and solve the disorder of Flink SQL upset Kafka sink data [arbitrary modification of business data]
[paper speed] solve the problems of underexposure and overexposure during imaging at the same time: deep replicating HDR transformation
NVIDIA Inspector 详细使用说明书
论文速读:Axial-DeepLab: Stand-Alone Axial-Attention for Panoptic Segmentation
【Unity基础知识】基础游戏单位GameObject中常用的属性和API
生成随机整数的两种方法
MySQL ON DELETE CASCADE(级联删除)[猿教程]
Acmix: on the integration of self attention and Revolution