标签:
主线程上调用异步
- (void)syncOnMainThread
{
NSLog(@"task2:%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"task1:%@",[NSThread currentThread]);
});
NSLog(@"task3:%@",[NSThread currentThread]);
}
死锁:使用同步函数在主队列执行,会造成线程死锁情况
- (void)testDeadLock
{
NSLog(@"task2:%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"task1:%@",[NSThread currentThread]);
});
NSLog(@"task3:%@",[NSThread currentThread]);
}
同步执行串行队列:不会开启新线程,按照顺序执行
- (void)syncChuanxing
{
dispatch_queue_t queue = dispatch_queue_create("gcd.queue", NULL);
//添加任务到队列中执行任务
dispatch_sync(queue, ^{
NSLog(@"task1:%@",[NSThread curren
标签:
原文地址:http://www.cnblogs.com/chongdongren/p/4718517.html