首页 技术 正文
技术 2022年11月15日
0 收藏 871 点赞 4,613 浏览 2507 个字

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]);
}
});}

iOS 多线程GCD的基本使用

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]);
});}

iOS 多线程GCD的基本使用

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]);
});
}

iOS 多线程GCD的基本使用

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]);
});
}

iOS 多线程GCD的基本使用

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]);
});
}

iOS 多线程GCD的基本使用

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");
}

造成“相互等待的死锁”

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295