- dispatch_queue_create
串行队列:先后顺序,依赖关系
- dispatch_get_global_queue
并行队列:不存在任何依赖关系,提高执行速度 - dispatch_group_create
分组任务:几个任务之间存在关系,最后一个任务依赖于前几个 - dispatch_once_t
一次:只执行一次 - dispatch_barrier_async
障碍:后几个需要前几个执行完,自己创建的队列,才能添加障碍, - dispatch_after
延迟: 需要延迟几秒 - dispatch_apply
反复执行:并发执行的特例版, 10任务的内容都是一样的
<pre><code>
- //串行队列, 线程同步,
//1.获取串行队列
//(1)获取系统创建好的串行队列,主队列,在主线程中执行
// dispatch_queue_t queue = dispatch_get_main_queue();
//(2)自己创建串行队列, 在子线程中执行
dispatch_queue_t queue = dispatch_queue_create("frank", DISPATCH_QUEUE_SERIAL);
//SERIAL串行
//2.往队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务1,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务2,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务3,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务4,当前线程%@", [NSThread currentThread]);
}); - //并行队列, 线程并发
- (IBAction)handleConcurrentQueue:(UIButton *)sender {
//1.获取并行队列
//(1)获取系统创建的并行队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//(2)自己创建并行队列
// dispatch_queue_t queue = dispatch_queue_create("dd", DISPATCH_QUEUE_CONCURRENT);
//2.往队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务1,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务2,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务3,当前线程%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务4,当前线程%@", [NSThread currentThread]);
});
//dispatch_sync 如果是添加的同步任务,任务同步执行全在主线程中处理,
}
//分组任务,
- (IBAction)handleGroup:(UIButton *)sender {
//1.获取并发队列,
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//2.创建分组
dispatch_group_t group = dispatch_group_create();
//3.往队列中添加分组任务
dispatch_group_async(group, queue, ^{
NSLog(@"请求0~1M数据, 当前线程%@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求1~2M数据, 当前线程%@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求2~3M数据, 当前线程%@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求3~4M数据, 当前线程%@", [NSThread currentThread]);
});
//所有分组任务完成之后触发
dispatch_group_notify(group, queue, ^{
//拼接分段数据
NSLog(@"拼接分段数据, 当前线程为%@", [NSThread currentThread]);
});
}
//整个应用程序运行期间,只执行一次
//初始化方法和单例 - (IBAction)handleOnce:(UIButton *)sender {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//存放只会执行一次的代码,比如单例对象的创建
});
}
//障碍 - (IBAction)handleBarrier:(UIButton *)sender {
//1.获取队列
// dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//如果要加障碍任务,必须要使用自己创建的队列
dispatch_queue_t queue = dispatch_queue_create("dd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"A 写入");
});
dispatch_async(queue, ^{
NSLog(@"B 写入");
});
dispatch_async(queue, ^{
NSLog(@"C 写入");
});
dispatch_barrier_async(queue, ^{
NSLog(@"障碍, 读取数据");
});
dispatch_async(queue, ^{
NSLog(@"D 写入");
});
dispatch_async(queue, ^{
NSLog(@"E 写入");
});
dispatch_async(queue, ^{
NSLog(@"F 写入");
});
}
//延迟
- (IBAction)handleDelay:(UIButton *)sender {
//1.
double second = 2.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//存放延迟10秒之后执行的任务
NSLog(@"Duck");
});
}
//重复执行的任务 - (IBAction)handleApply:(UIButton *)sender {
//并行队列
// dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//串行队列
dispatch_queue_t queue = dispatch_queue_create("cc", DISPATCH_QUEUE_SERIAL);
NSArray *arr = @[@"aa", @"bb", @"cc", @"dd", @"ee", @"ff", @"gg", @"hh", @"ii", @"kk"];
//如果每次只执行一个任务,则在主线程中执行,如果并发执行多个,在主线程之外,还会分配子线程
dispatch_apply(arr.count, queue, ^(size_t index) {
NSLog(@"任务%lu 当前线程%@", index + 1, [NSThread currentThread]);
NSLog(@"%@", arr[index]);
});
}
</code>