- (void)gcdTest{
//全局队列,负责调度任务
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
NSLog(@"%@",[NSThread currentThread]);
void (^task)() = ^{
NSLog(@"%@",[NSThread currentThread]);
};
// dispatch_sync(q, task);//{number = 1, name = main}
dispatch_async(q, task);//<NSThread: 0x7f9d8b536990>{number = 2, name = (null)}
}
- (void)gcdTest3{
dispatch_async(dispatch_get_global_queue(0, 0), ^{NSLog(@"%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@",[NSThread currentThread]);
});
});
}
下载图片
- (void)gcdDownImage{
NSLog(@"0--%@",[NSThread currentThread]);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"1--%@",[NSThread currentThread]);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.easemob.com/files/vi/cs_180x1359.png"]];
NSLog(@"2--%@",[NSThread currentThread]);
UIImage *image = [UIImage imageWithData:data];
NSLog(@"3--%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"4--%@",[NSThread currentThread]);
self.imageView.image = image;
});
});
}