自己创建队列中 并发+异步
-(void)test5
{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("com.gcd-group.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
for(NSInteger i = 0; i < 1000; i++) {
if(i == 999) {
NSLog(@"11111111");
}
}
});
dispatch_group_async(group, queue, ^{
NSLog(@"22222222");
});
dispatch_group_async(group, queue, ^{
NSLog(@"33333333");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"done");
});
}
全局并发队列异步+信号量(是否创建队列)
-(void)test4
{
//crate的value表示,最多几个资源可访问
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"%@",semaphore);
//任务1
dispatch_async(quene, ^{
NSLog(@"run task 01");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 1");
sleep(1);
NSLog(@"complete task 1");
dispatch_semaphore_signal(semaphore);
});
//任务2
dispatch_async(quene, ^{
NSLog(@"run task 02");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 2");
sleep(1);
NSLog(@"complete task 2");
dispatch_semaphore_signal(semaphore);
});
//任务3
dispatch_async(quene, ^{
NSLog(@"run task 03");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"run task 3");
sleep(1);
NSLog(@"complete task 3");
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_enter dispatch_group_leave 使用
-(void)test3
{
dispatch_queue_t conCurrentQueue = dispatch_queue_create("conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(conCurrentQueue, ^{
NSLog(@"任务一开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"任务一快结束");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(conCurrentQueue, ^{
NSLog(@"任务二开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"任务二快结束");
dispatch_group_leave(group);
});
dispatch_group_notify(group, conCurrentQueue, ^{
NSLog(@"被通知任务开始");
});
NSLog(@"-----");
}
dispatch_group_wait
-(void)test2{
dispatch_queue_t conCurrentQueue = dispatch_queue_create("conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, conCurrentQueue, ^{
NSLog(@"任务一开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"任务一快结束");
});
dispatch_group_async(group, conCurrentQueue, ^{
NSLog(@"任务二开始");
[NSThread sleepForTimeInterval:6];
NSLog(@"任务二快结束");
});
dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC));
long result=dispatch_group_wait(group, time);
if (result==0) {
NSLog(@"组的block全部执行完成");
}
else{
NSLog(@"组的block没有全部执行完成,是timeout返回");
}
NSLog(@"-----------");
}
dispatch_block_cancel
-(void)test1
{
#pragma mark ---------- block 和 cancle
dispatch_queue_t myQueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
// 耗时任务
dispatch_block_t firstBlock = dispatch_block_create(0, ^{
NSLog(@"开始第一个任务:%d",[NSThread isMainThread]);
[NSThread sleepForTimeInterval:1.5f];
NSLog(@"结束第一个任务");
});
// 耗时任务
dispatch_block_t secBlock = dispatch_block_create(0, ^{
NSLog(@"开始第二个任务:%d",[NSThread isMainThread]);
[NSThread sleepForTimeInterval:2.f];
NSLog(@"结束第二个任务");
});
dispatch_async(myQueue, firstBlock);
dispatch_async(myQueue, secBlock);
// 等待 1s,让第一个任务开始运行,因为myQueue是串行队列,遵守fifo先进先出的规则,所以必须先执行完block1,才能执行block2
[NSThread sleepForTimeInterval:1];
NSLog(@"休眠:%d",[NSThread isMainThread]);
dispatch_block_cancel(firstBlock);
NSLog(@"准备取消第一个任务");
dispatch_block_cancel(secBlock);
NSLog(@"准备取消第二个任务");
}
网友评论