- (void)viewDidLoad { [super viewDidLoad]; // 获得全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, 0); // 执行同步任务 dispatch_sync(q, ^{ // 让执行该任务的线程休眠2s [NSThread sleepForTimeInterval:2.0]; NSLog(@"block - %@",[NSThread currentThread]); }); NSLog(@"viewDidLoad - %@",[NSThread currentThread]); }
- (void)viewDidLoad { [super viewDidLoad]; // 获得全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, 0); // 执行异步任务 dispatch_async(q, ^{ // for (int i = 0; i<10; i++) { NSLog(@"block - %@ - %d",[NSThread currentThread],i); } }); NSLog(@"viewDidLoad - %@",[NSThread currentThread]); }
- (void)viewDidLoad { [superviewDidLoad]; //获得全局队列 dispatch_queue_t q =dispatch_get_global_queue(0, 0); //执行异步任务 for (int i = 0; i< 10; i++) { dispatch_async(q, ^{ NSLog(@"block - %@ - %d",[NSThreadcurrentThread],i); }); } NSLog(@"viewDidLoad - %@",[NSThreadcurrentThread]); }
原文地址:http://blog.csdn.net/u011058732/article/details/44464569