第一种:dispatch_queue
(1)Serial Dispatch Queue --- 等待现在正在执行的任务处理结束(串行)
(2)Concurrent Dispatch Queue --- 不等待现在正在执行的任务处理结束(并行、并发)
dispatch_queue_t queue1 = dispatch_queue_create("com.wxhl.gcd.Queue1", NULL);
dispatch_queue_t queue2 = dispatch_queue_create("com.wxhl.gcd.Queue2", DISPATCH_QUEUE_CONCURRENT); //并行的queue
//(2)创建要执行的任务,加到queue中执行
dispatch_async(queue2, ^{
for (int i = 0; i < 50; i ++) {
NSLog(@"GCD : %d", i);
}
});
dispatch_async(queue2, ^{
for (int i = 0; i < 50; i ++) {
NSLog(@"GCD2------ : %d", i);
}
});
//dispatch_after 是过一段时间后,把任务加入到队列中
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC);
//NSEC_PER_SEC 秒
//NSEC_PER_MSEC 毫秒
//NSEC_PER_USEC 微秒
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"task 1");
});
//第二种方法
dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 6ull*NSEC_PER_SEC), dispatch_get_main_queue(),NULL, fun);
//自己使用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5ull * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"task 3");
});
}
void fun(){
NSLog(@"task 2");
}
//并行队列执行任务,在并行队列中加入多个串行队列
//disptch_group
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_group_async(group, queue, ^{
NSLog(@"task 01");
});
dispatch_group_async(group, queue, ^{
NSLog(@"task 02");
});
dispatch_group_async(group, queue, ^{
sleep(6);//等待6秒执行任务3 目的:测试等待时间的dispatch_group_wait这个方法
NSLog(@"task 03");
});
dispatch_group_async(group, queue, ^{
sleep(2);
NSLog(@"task 04");
});
//监视函数
//监视队列中得任务结束,执行block中得任务
dispatch_group_notify(group, queue, ^{
NSLog(@"done");
});
//等待时间
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 5ull*NSEC_PER_SEC);
//dispatch_group_wait 指定时间后,看你一眼queue是否执行完毕
//如果执行完返回0
//没有执行完,返回非0值
long result = dispatch_group_wait(group, time);
if (result == 0) {
NSLog(@"finish");
}else{
NSLog(@"not finish");
}
}
+ (ShareOnce *)shanreInstence{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ShareOnce alloc] init];
NSLog(@"只执行1次");
});
return instance;
}
//alloc会自动调用allocWithZone这个方法
//zone空间 自动分配内存空间 创建对象
+ (id)allocWithZone:(struct _NSZone *)zone{
if (instance == nil) {
instance = [super allocWithZone:zone];
}
return instance;
}
//使用
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_semaphore_t dsema = dispatch_semaphore_create(1);
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 1000; i++) {
dispatch_async(queue, ^{
dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
[array addObject:[NSNumber numberWithInt:i]];
dispatch_semaphore_signal(dsema);
});
}
NSLog(@"%@", array);
//dispatch sync
//
//async: asynchronous 将任务异步的追加到队列中
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"async");
});
//sync: synchronous 将任务同步的追加到队列中(等队列中的任务执行完,再将任务追加到队列)
//是同步追加,不是任务同步执行,在串行队列中,任务才同步执行
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"sync");
});
//dispatch_sync的问题:容易产生死锁
//示例1:
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"hello");
});
NSLog(@"主线程");
//上述代码在主队列中执行指定的block,等待其执行结束
//而主队列中本来就在执行上述代码,无法执行追加的block
//示例2:
//串行的队列
dispatch_queue_t queue = dispatch_queue_create("com.wxhl.GCD.queue", NULL);
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
NSLog(@"串行队列");
});
});