- (void)test1{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
dispatch_async(q1, ^{
NSLog(@"4");
});
dispatch_async(q1, ^{
NSLog(@"5");
});
NSLog(@"6");
NSLog(@"---end---");
}
运行结果
2018-02-22 13:31:34.085298+0800 T1122[23973:291285] ---begin---
2018-02-22 13:31:34.085421+0800 T1122[23973:291285] 1
2018-02-22 13:31:34.085514+0800 T1122[23973:291285] 6
2018-02-22 13:31:34.085526+0800 T1122[23973:291312] 2
2018-02-22 13:31:34.085535+0800 T1122[23973:291311] 3
2018-02-22 13:31:34.085615+0800 T1122[23973:291285] ---end---
2018-02-22 13:31:34.085539+0800 T1122[23973:291316] 4
2018-02-22 13:31:34.085543+0800 T1122[23973:291315] 5
或者
2018-02-22 13:36:34.332409+0800 T1122[24445:298978] ---begin---
2018-02-22 13:36:34.332563+0800 T1122[24445:298978] 1
2018-02-22 13:36:34.332741+0800 T1122[24445:299027] 2
2018-02-22 13:36:34.332759+0800 T1122[24445:299026] 3
2018-02-22 13:36:34.332771+0800 T1122[24445:298978] 6
2018-02-22 13:36:34.332782+0800 T1122[24445:299028] 4
2018-02-22 13:36:34.332786+0800 T1122[24445:299037] 5
2018-02-22 13:36:34.332941+0800 T1122[24445:298978] ---end---
或者
2018-02-22 13:36:03.107987+0800 T1122[24385:297811] ---begin---
2018-02-22 13:36:03.108196+0800 T1122[24385:297811] 1
2018-02-22 13:36:03.108371+0800 T1122[24385:297811] 6
2018-02-22 13:36:03.108385+0800 T1122[24385:297852] 2
2018-02-22 13:36:03.108415+0800 T1122[24385:297851] 3
2018-02-22 13:36:03.108424+0800 T1122[24385:297849] 4
2018-02-22 13:36:03.108440+0800 T1122[24385:297850] 5
2018-02-22 13:36:03.108524+0800 T1122[24385:297811] ---end---
结语:dispatch_async 直接返回,具体执行顺序不确定。
- (void)test2{
dispatch_queue_t q1 = dispatch_get_main_queue();
NSLog(@"---begin---");
NSLog(@"1");
dispatch_sync(q1, ^{
NSLog(@"2");
});
NSLog(@"6");
NSLog(@"---end---");
}
运行结果
2018-02-22 13:39:23.682319+0800 T1122[24702:303022] ---begin---
2018-02-22 13:39:23.682572+0800 T1122[24702:303022] 1
(lldb)
卡死、系统报错
结语: 使用dispatch_sync 是把block放到指定的queue上面执行,但是会等待这个block执行完毕才会返回,阻塞当前queue直到sync函数返回。
官方文档指出:dispatch_sync的当前执行队列与提交block执行的目标队列相同时将造成死锁。
- (void)test3{
dispatch_queue_t q1 = dispatch_get_main_queue();
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
NSLog(@"6");
NSLog(@"---end---");
}
运行结果
2018-02-22 13:46:43.126787+0800 T1122[25363:316021] ---begin---
2018-02-22 13:46:43.126933+0800 T1122[25363:316021] 1
2018-02-22 13:46:43.127034+0800 T1122[25363:316021] 6
2018-02-22 13:46:43.127131+0800 T1122[25363:316021] ---end---
2018-02-22 13:46:43.133422+0800 T1122[25363:316021] 2
2018-02-22 13:46:43.133541+0800 T1122[25363:316021] 3
- (void)test4{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_SERIAL);
dispatch_async(q1, ^{
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
NSLog(@"6");
NSLog(@"---end---");
});
}
运行结果
2018-02-22 13:54:18.898121+0800 T1122[26106:328509] ---begin---
2018-02-22 13:54:18.898280+0800 T1122[26106:328509] 1
2018-02-22 13:54:18.898394+0800 T1122[26106:328509] 6
2018-02-22 13:54:18.898496+0800 T1122[26106:328509] ---end---
2018-02-22 13:54:18.898589+0800 T1122[26106:328509] 2
2018-02-22 13:54:18.898712+0800 T1122[26106:328509] 3
结语: 同一个串行队列,dispatch_async 任务直接追加到最后执行
并行队列 任务执行随机
- (void)test5{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
//遍历
dispatch_apply(6, q1, ^(size_t index) {
NSLog(@"%zd ---",index);
});
NSLog(@"---end---");
}
运行结果
2018-02-22 13:58:58.726618+0800 T1122[26519:334474] ---begin---
2018-02-22 13:58:58.729158+0800 T1122[26519:334525] 1 ---
2018-02-22 13:58:58.729158+0800 T1122[26519:334474] 0 ---
2018-02-22 13:58:58.729199+0800 T1122[26519:334528] 3 ---
2018-02-22 13:58:58.729175+0800 T1122[26519:334527] 2 ---
2018-02-22 13:58:58.729254+0800 T1122[26519:334526] 4 ---
2018-02-22 13:58:58.729283+0800 T1122[26519:334541] 5 ---
2018-02-22 13:58:58.729486+0800 T1122[26519:334474] ---end---
- (void)test6{
dispatch_group_t g1 = dispatch_group_create();
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
NSLog(@"");
}
NSLog(@"--operation1--");
});
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
NSLog(@"");
}
NSLog(@"--operation2--");
});
dispatch_group_notify(g1, q1, ^{
// 等前面的异步操作都执行完毕后,回到q1...
NSString *str = @"等前面的异步操作都执行完毕后,回到q1...";
NSLog(@"%@",str);
});
NSLog(@"---end---");
}
2018-02-22 14:07:32.572516+0800 T1122[27367:349489] ---begin---
2018-02-22 14:07:32.572709+0800 T1122[27367:349489] ---end---
2018-02-22 14:07:32.572716+0800 T1122[27367:349532] --operation1--
2018-02-22 14:07:32.572724+0800 T1122[27367:349535] --operation2--
2018-02-22 14:07:32.572892+0800 T1122[27367:349535] 等前面的异步操作都执行完毕后,回到q1...
- (void)test7{
dispatch_group_t g1 = dispatch_group_create();
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation1--");
});
dispatch_barrier_async(q1, ^{
NSLog(@"QQQQQQ");
});
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation2--");
});
dispatch_group_notify(g1, q1, ^{
// 等前面的异步操作都执行完毕后,回到线程...
NSString *str = @"等前面的异步操作都执行完毕后,回到线程...";
NSLog(@"%@",str);
});
NSLog(@"---end---");
}
运行结果
2018-02-22 14:11:23.950412+0800 T1122[27790:357902] ---begin---
2018-02-22 14:11:23.950634+0800 T1122[27790:357902] ---end---
2018-02-22 14:11:23.950643+0800 T1122[27790:357932] --operation1--
2018-02-22 14:11:23.950789+0800 T1122[27790:357932] QQQQQQ
2018-02-22 14:11:23.950899+0800 T1122[27790:357932] --operation2--
2018-02-22 14:11:23.951060+0800 T1122[27790:357932] 等前面的异步操作都执行完毕后,回到线程...
栅栏函数
源文件
//
// ViewController.m
// T1122
//
// Created by TJ on 2018/2/22.
// Copyright © 2018年 TJ. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self test7];
}
- (void)test1{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
dispatch_async(q1, ^{
NSLog(@"4");
});
dispatch_async(q1, ^{
NSLog(@"5");
});
NSLog(@"6");
NSLog(@"---end---");
}
- (void)test2{
dispatch_queue_t q1 = dispatch_get_main_queue();
NSLog(@"---begin---");
NSLog(@"1");
dispatch_sync(q1, ^{
NSLog(@"2");
});
NSLog(@"6");
NSLog(@"---end---");
}
- (void)test3{
dispatch_queue_t q1 = dispatch_get_main_queue();
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
NSLog(@"6");
NSLog(@"---end---");
}
- (void)test4{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_SERIAL);
dispatch_async(q1, ^{
NSLog(@"---begin---");
NSLog(@"1");
dispatch_async(q1, ^{
NSLog(@"2");
});
dispatch_async(q1, ^{
NSLog(@"3");
});
NSLog(@"6");
NSLog(@"---end---");
});
}
- (void)test5{
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
//遍历
dispatch_apply(6, q1, ^(size_t index) {
NSLog(@"%zd ---",index);
});
NSLog(@"---end---");
}
- (void)test6{
dispatch_group_t g1 = dispatch_group_create();
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation1--");
});
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation2--");
});
dispatch_group_notify(g1, q1, ^{
// 等前面的异步操作都执行完毕后,回到主线程...
NSString *str = @"等前面的异步操作都执行完毕后,回到主线程...";
NSLog(@"%@",str);
});
NSLog(@"---end---");
}
- (void)test7{
dispatch_group_t g1 = dispatch_group_create();
dispatch_queue_t q1 = dispatch_queue_create("com.ym", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---begin---");
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation1--");
});
dispatch_barrier_async(q1, ^{
NSLog(@"QQQQQQ");
});
dispatch_group_async(g1, q1, ^{
for (int i = 0; i < 1000; i++) {
// NSLog(@"");
}
NSLog(@"--operation2--");
});
dispatch_group_notify(g1, q1, ^{
// 等前面的异步操作都执行完毕后,回到主线程...
NSString *str = @"等前面的异步操作都执行完毕后,回到主线程...";
NSLog(@"%@",str);
});
NSLog(@"---end---");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论