- (void)viewDidLoad {
[super viewDidLoad];
// [self async_concurrent_queue];
// [self async_serial_queue];//异步串行创建一个新的线程 但是只在一个线程中执行
// [self sync_concurrent_queue];//同步执行不创建新线程
// [self sync_serial_queue];//同步执行不创建新线程
// [self setTargetQueue];//赋予队列优先级
// [self queueAfter];//延时加载
// [self dispatchGroup];//分组管理任务,能监测整个分组中的任务执行状态
// [self dispatch_apply];//指定次数将block添加到指定queue中
// [self suspend_resume];//暂停和开始线程
[self test];
}
//异步并行线程
- (void)async_concurrent_queue{
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5");
NSLog(@"%@",[NSThread currentThread]);
});
}
//异步串行线程
- (void)async_serial_queue{
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5");
NSLog(@"%@",[NSThread currentThread]);
});
}
//同步并行
- (void)sync_concurrent_queue{
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2");
});
dispatch_sync(queue, ^{
NSLog(@"3");
});
dispatch_sync(queue, ^{
NSLog(@"4");
});
dispatch_sync(queue, ^{
NSLog(@"5");
NSLog(@"%@",[NSThread currentThread]);
});
}
//同步串行
- (void)sync_serial_queue{
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2");
});
dispatch_sync(queue, ^{
NSLog(@"3");
});
dispatch_sync(queue, ^{
NSLog(@"4");
});
dispatch_sync(queue, ^{
NSLog(@"5");
NSLog(@"%@",[NSThread currentThread]);
});
}
//设置队列优先级
- (void)setTargetQueue{
//创建一个队列
dispatch_queue_t myqueue = dispatch_queue_create("com.example.myqueue", DISPATCH_QUEUE_CONCURRENT);
//创建目标队列
dispatch_queue_t targetQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
//myqueue 使用 targetQueue的优先级策略
dispatch_set_target_queue(myqueue, targetQueue);
}
//延时处理
- (void)queueAfter{
//设置定时器时间
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"延时3秒调用");
});
}
//分组管理任务,能够整个分组中的执行结果
- (void)dispatchGroup{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1");
});
dispatch_group_async(group, queue, ^{
NSLog(@"2");
});
dispatch_group_async(group, queue, ^{
NSLog(@"3");
});
dispatch_group_notify(group, queue, ^{//完成线程中的人物 通知得到响应
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"完成任务啦");
});
});
}
//该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束 //跟for循环类似
- (void)dispatch_apply{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_apply(10, queue, ^(size_t index) {
NSLog(@"%zu", index);
});
}
//暂停queue 开始queue
- (void)suspend_resume{
dispatch_queue_t queue = dispatch_queue_create("com.test.gcd", DISPATCH_QUEUE_SERIAL);
//提交第一个block,延时5秒打印。
dispatch_async(queue, ^{
sleep(5);
NSLog(@"After 5 seconds...");
});
//提交第二个block,也是延时5秒打印
dispatch_async(queue, ^{
sleep(5);
NSLog(@"After 5 seconds again...");
});
//延时一秒
NSLog(@"sleep 1 second...");
sleep(1);
//挂起队列
NSLog(@"suspend...");
dispatch_suspend(queue);
//延时10秒
NSLog(@"sleep 10 second...");
sleep(10);
//恢复队列
NSLog(@"resume...");
dispatch_resume(queue);
// dispatch_suspend并不会立即暂停正在运行的block,而是在当前block执行完成后,暂停后续的block执行。
}
//单例模式
+ (instancetype)sharedManager{
static dispatch_once_t onceToken;
static ViewController * vc = nil;
dispatch_once(&onceToken, ^{
vc = [[ViewController alloc]init];
});
return vc;
}
#pragma mark --------------死锁
//!!!只有使用了dispatch_sync函数分发任务到 当前队列 才会导致死锁。
//对于dispatch_sync同步等待执行函数要谨慎使用。
//在主线程队列调用dispatch_sync 就会造成死锁
//calling this function and targeting the current queue resuls in deadlock
- (void)test{
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
/*
主线程停在同步提交任务这一步,导致block不会执行,
block不执行,同步提交任务就会停留在主线程中一直等待,导致死锁
当前队列是主队列,block被提交到了当前队列
*/
NSLog(@"Hello?");
});
// 当前队列是主队列,使用dispatch_sync函数,block被提交到了当前队列
dispatch_async(mainQueue, ^{
dispatch_sync(mainQueue, ^{
/*
同样由于主队列任务互相等待造成死锁
*/
});
});
}