1.GCD中的一些基础数据类型
/*
GCD中一些基础数据类型
dispatch_time_t:dispatch_walltime获得真实时间(绝对时间),dispatch_time获得马赫时间
*/
dispatch_time_t time = dispatch_walltime(NULL, 0);
NSLog(@"%lld", time);
dispatch_time_t time2 = dispatch_time(0, 1 * NSEC_PER_SEC);
NSLog(@"%lld", time2);
dispatch_time_t time3 = DISPATCH_TIME_NOW;
NSLog(@"%lld", time3);
2.串行、并行和同步、异步的概念
/*
串行(serial):只有在上一个任务执行完才会执行下一个任务
并行(concurrent):多个任务同时执行
*/
/*
同步(sync):代码执行完才会往下走(返回值,return),且不会创建新线程,而是在主线程中的,即时我们传了其他队列参数也不会创建
异步(async):代码不需要执行完,直接可以执行下面的代码
*/
3.主线程下死锁的产生
/*
主线程,是一个系统自带的串行线程,往主线程添加任务,则需要等当前任务完成,即viewDidLoad方法走完,所以这里Log是后于直接Log的
死锁:在主线程中同步给主线程添加任务,则产生死锁
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"异步添加任务到主线程:%@", [NSThread currentThread]);
});
*/
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"异步添加任务到主线程:%@", [NSThread currentThread]);
});
NSLog(@"直接在主线程中执行:%@", [NSThread currentThread]);
4.Object-c SDK自带的4个全局并行队列
/*
系统自带的4个全局线程(并行),通过以下4个宏设置线程优先级
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
优先级高的先执行,但是执行完的速度并不一定
同一优先级的队列中的任务,执行顺序不定
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSLog(@"start_low_1");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSLog(@"start_low_2");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSLog(@"start_low_3");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"start_default");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"start_high");
NSLog(@"%@", [NSThread currentThread]);
});
5.自定义队列
/*
自己创建线程队列,一般不创建并行的,因为系统自带的已经够了
串行队列-异步执行:任务会根据添加顺序执行,但是不阻碍下面的其他代码执行
串行队列-同步执行:基本和直接在主线程中写代码没什么区别
*/
dispatch_queue_t my_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(my_queue, ^{
NSLog(@"start_my_1");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(my_queue, ^{
NSLog(@"start_my_2");
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"start_high_2");
NSLog(@"%@", [NSThread currentThread]);
});
6.设置队列优先级,仅能设置自定义队列
/*
dispatch_set_target_queue
把我们的队列添加到全局队列中执行,则我们队列的优先级就会和全局队列一样
通常用这种方式来改变我们自己创建的队列的优先级
*/
dispatch_set_target_queue(my_queue, dispatch_get_global_queue(0, 0));
7.GCD中的通知调用Api
/*
group和notify
1.由Log可见,over都是在1,2之后的确实起到了通知的作用
2.over2并没有等待两个异步请求完成就已经调用,这是因为请求本身是异步的,在调用后直接返回了,所以dispatch_group_async认为该任务已经执行完,所以无作用
3.要把一个方法或函数作为任务添加到组中等待通知,应该使用dispatch_group_enter()和dispatch_group_leave()
*/
// 1
dispatch_queue_t queue = dispatch_queue_create("my1", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1");
});
dispatch_group_async(group, queue, ^{
NSLog(@"2");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"over");
});
// 2
dispatch_queue_t queue2 = dispatch_queue_create("my2", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group2 = dispatch_group_create();
dispatch_group_async(group2, queue2, ^{
[self request1:^{
}];
});
dispatch_group_async(group2, queue2, ^{
[self request2:^{
}];
});
dispatch_group_notify(group2, queue2, ^{
NSLog(@"over2");
});
// 3
dispatch_queue_t queue3 = dispatch_queue_create("my3", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group3 = dispatch_group_create();
dispatch_group_enter(group3);
[self request1:^{
dispatch_group_leave(group3);
}];
dispatch_group_enter(group3);
[self request2:^{
dispatch_group_leave(group3);
}];
dispatch_group_notify(group3, queue3, ^{
NSLog(@"over3");
});
8.阻塞线程
/*
dispatch_barrier_async
dispatch_barrier_async传的队列参数必须是用户自己创建的,否则没用
当用户想要先完成任务1,2,中途要等待任务3完成后,才能开始4,5,其中12,45是并发的
dispatch_barrier_async和dispatch_barrier_sync的共同点在于都会阻挡在dispatch_barrier之后添加的任务,并延时调用(等待它之前的任务回调完成),不同点在于async是不阻挡主线程的,而sync是阻挡主线程的
*/
dispatch_queue_t barrier_queue = dispatch_queue_create("zk_barrier_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_1");
});
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_2");
});
dispatch_barrier_async(barrier_queue, ^{
NSLog(@"barrier_3");
});
NSLog(@"aa");
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_4");
});
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_5");
});
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_1_1");
});
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_2_2");
});
dispatch_barrier_sync(barrier_queue, ^{
NSLog(@"barrier_3_3");
});
NSLog(@"bb");
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_4_4");
});
dispatch_async(barrier_queue, ^{
NSLog(@"barrier_5_5");
});
9.延迟调用
/*
dispatch_after
延迟调用,一旦开始,就无法停下,一般只用在主队列中
*/
dispatch_after(dispatch_time(dispatch_walltime(NULL, 0), 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"dispatch_after");
});
dispatch_after(dispatch_time(0, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"dispatch_after2");
});
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
NSLog(@"dispatch_after2");
});
10.遍历
/*
dispatch_apply
无序遍历,会开启多个线程,但会阻碍主线程的运行
*/
NSArray *array = @[@1, @2, @3, @4, @5];
dispatch_apply(array.count, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"%@---%@", [NSThread currentThread], array[index]);
});
NSLog(@"主线程end");