gcd常见用法
- (IBAction)aaaa:(id)sender {
// [self gcdBlockCancel];
// CFRunLoopGetCurrent();
// for (int i = 0; i < 3; i++) {
// [self gcdCancel];
// }
// [self queueDrop];
// [self sourceDemo];
// NSOperation *op;
// [self testSemaphore];
//[self testBarrier];
[self sourceDemo];
// [self timerDemo];
//消息转发
}
- (void)timerDemo {
//gcd timer
}
- (void)testBarrier {
__block dispatch_queue_t queue = dispatch_queue_create("hello", 0);
dispatch_async(queue, ^{
NSLog(@"hello 111");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"hello 22222");
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"在全局队列操作");
//dispatch_suspend(queue);
//queue = NULL;
});
dispatch_async(queue, ^{
NSLog(@"准备挂起");
dispatch_suspend(queue);
NSLog(@"挂起了....");
//queue = NULL;
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"取消挂起,恢复队列..");
dispatch_resume(queue);
});
dispatch_barrier_sync(queue, ^{
NSLog(@"该轮到我了....");
});
}
- (void)testSemaphore {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_queue_t queue = dispatch_queue_create("hello", 0);
dispatch_async(queue, ^{
for (int i = 0; i < 10; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"i====%d",i);
sleep(1);
dispatch_semaphore_signal(semaphore);
});
}
});
}
- (void)testGroup {
//group
dispatch_group_t group = dispatch_group_create();
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int i = 0; i < 10; i++)
{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(group, queue, ^{
NSLog(@"%i",i);
sleep(1);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_group_notify(group, queue, ^{
NSLog(@"finished...");
});
}
- (void)sourceDemo {
dispatch_queue_t queue = dispatch_queue_create("com.example.gcd.MyConcurrentDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
// dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
// dispatch_source_set_event_handler(timer, ^{
// NSLog(@"aaa");
// });
// dispatch_resume(timer);
// dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
// 0, 0, queue);
//
//
// dispatch_block_t block1 = dispatch_block_create(0, ^{
//// sleep(5);
// NSLog(@"block1 %@",[NSThread currentThread]);
//
// });
//
//
// dispatch_source_set_event_handler(source, block1);
//
// dispatch_source_set_event_handler(source, ^{
// // Get some data from the source variable, which is captured
// // from the parent context.
// size_t estimated = dispatch_source_get_data(source);
// // Continue reading the descriptor...
// NSLog(@"aaa");
// });
// dispatch_resume(source);
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, queue);
dispatch_async(queue, ^{
__block BOOL shouldStop = NO;
dispatch_source_set_event_handler(source, ^{
size_t estimated = dispatch_source_get_data(source);
// Continue reading the descriptor...
NSLog(@"estimated===%ld",estimated);
if (estimated == 2) {
NSLog(@"收到source......");
shouldStop = YES;
}
NSLog(@"马上停止队列...");
//dispatch_source_cancel(source);
dispatch_suspend(queue);
});
dispatch_resume(source);
for (NSInteger i = 0; i < 99; i++) {
[NSThread sleepForTimeInterval:0.5];
if (shouldStop) {
NSLog(@"任务取消了...");
dispatch_sync(dispatch_get_main_queue(), ^{
//更新UI
NSLog(@"更新UI....");
});
break;
}
NSLog(@"index====%ld",i);
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//网络请求
[NSThread sleepForTimeInterval:5];
dispatch_source_merge_data(source, 2); //通知队列
});
}
- (void)queueDrop {
dispatch_queue_t myConcurrentDispatchQueue = dispatch_queue_create("com.example.gcd.MyConcurrentDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentDispatchQueue, ^{
NSLog(@"block on myConcurrentDispatchQueue");
});
//dispatch_release(myConcurrentDispatchQueue);
}
- (void)gcdBlockCancel{
dispatch_queue_t queue = dispatch_queue_create("com.gcdtest.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_block_t block1 = dispatch_block_create(0, ^{
sleep(5);
NSLog(@"block1 %@",[NSThread currentThread]);
});
dispatch_block_t block2 = dispatch_block_create(0, ^{
NSLog(@"block2 %@",[NSThread currentThread]);
});
dispatch_block_t block3 = dispatch_block_create(0, ^{
NSLog(@"block3 %@",[NSThread currentThread]);
});
dispatch_async(queue, block1);
dispatch_async(queue, block2);
dispatch_block_cancel(block3);
}
- (void)gcdCancel{
dispatch_queue_t queue = dispatch_queue_create("demo.test", 0);//dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block BOOL isCancel = NO;
dispatch_async(queue, ^{
NSLog(@"任务001 %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务002 %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务003 %@",[NSThread currentThread]);
isCancel = YES;
});
dispatch_async(queue, ^{
// 模拟:线程等待3秒,确保任务003完成 isCancel=YES
sleep(3);
if(isCancel){
NSLog(@"任务004已被取消 %@",[NSThread currentThread]);
}else{
NSLog(@"任务004 %@",[NSThread currentThread]);
}
});
}