标签:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan:%@",[NSThread currentThread]);
NSLog(@"耗时操作1,%@",[NSThread currentThread]);
NSLog(@"耗时操作2,%@",[NSThread currentThread]);
NSLog(@"耗时操作3,%@",[NSThread currentThread]);
NSLog(@"touchesEnd:%@",[NSThread currentThread]);
}
// 同步函数 + 主队列
// 结论:主线程中的任务和主队列中的任务相互等待.谁也无法执行.
- (void)test3
{
// 同步函数 + 主队列
// 结论:主线程中的任务和主队列中的任务相互等待.谁也无法执行.
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"耗时操作1,%@",[NSThread currentThread]);
});
}
// 同步函数+并发队列
// 结论:1.主线程执行/没有开启新线程
// 2.串行执行
- (void)test2
{
// 同步函数 + 并发队列
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"耗时操作1,%@",[NSThread currentThread]);
});
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"耗时操作2,%@",[NSThread currentThread]);
});
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"耗时操作3,%@",[NSThread currentThread]);
});
}
// 同步函数+串行队列
// 结论:1.主线程执行/没有开启新线程
// 2. 串行执行
- (void)test1
{
// 创建串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("itcast", DISPATCH_QUEUE_SERIAL);
// 同步函数+串行队列
// 结论:1.主线程执行/没有开启新线程
// 2. 串行执行
dispatch_sync(serialQueue, ^{
NSLog(@"耗时操作1,%@",[NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"耗时操作2,%@",[NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"耗时操作3,%@",[NSThread currentThread]);
});
}
@end
标签:
原文地址:http://www.cnblogs.com/R-X-L/p/4777561.html