今天回顾一下GCD多线程的使用。
在GCD中有两个非常重要的概念:任务和队列。
任务有两种执行方式: 同步执行 和 异步执行,二者的区别是是否会创建新的线程。
同步任务(同步操作):dispatch_sync
异步任务(异步操作):dispatch_async
队列用于存放任务,队列有三种:串行队列、并发队列、主队列
下面就分别列举下这几种任务和队列的组合
1.异步操作 + 串行队列
/**
* 异步操作 + 串行队列
* result:开启一个子线程 顺序执行
*/
- (void)asyncandserial {
// 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}
打印LOG
结果:开启一个子线程 顺序执行
2.异步操作 + 并发队列
/**
* 异步操作 + 并发队列
* result:开启多个子线程,并发执行
*/
- (void)asyncandconcurrent {
// 创建全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}
打印LOG
结果:开启多个子线程,并发执行
3.异步操作 + 主队列
/**
* 异步操作 + 主队列
* result:在主线程 顺序执行
*/
- (void)asyncandmain {
// 创建主线程
dispatch_queue_t queue = dispatch_get_main_queue();
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启异步操作
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}
打印LOG
结果:在主线程 顺序执行
4.同步操作 + 串行队列
/**
* 同步操作 + 串行队列
* result:主线程 顺序执行
*/
- (void)syncandserial {
// 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}
打印LOG
结果:在主线程 顺序执行
5.同步操作 + 并发队列
/**
* 同步操作 + 并发队列
* result:主线程 顺序执行
*/
- (void)syncandconcurrent {
// 创建并发队列
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}
结果:在主线程 顺序执行
6.同步操作 + 主队列
/**
* 同步操作 + 主队列
*/
- (void)syncandmain {
// 创建主队列
dispatch_queue_t queue = dispatch_get_main_queue();
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"one--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"two--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"three--%@",[NSThread currentThread]);
}
});
// 开启同步操作
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"four--%@",[NSThread currentThread]);
}
});
}