dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSData *data = [NSDatadata WithContentsOfURL:[NSURLURLWithString:self.imagaPaths[indexPath.row]]];
UIImage *image = [UIImageimage WithData:data];
//返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image= image;
[cell setNeedsLayout];
});
{
[super viewDidLoad];
//方式一开启一个线程
NSOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(newThreadAction)object:nil];
//方式二开启一个线程
NSOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i=0; i<5; i++) {
[NSThread sleepForTimeInterval:.5];
NSLog(@"线程2--%d",i);
}
}];
//创建线程队列,默认创建出来的线程队列为并行队列
NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
//设置最大同时执行的线程数量
[myQueue setMaxConcurrentOperationCount:1];
//给op1添加一个依赖依赖op2
[op1 addDependency:op2];
//将线程添加到队列,线程添加到队列后才会执行
[myQueue addOperation:op1];
[myQueue addOperation:op2];
}
- (void)newThreadAction{
for (int i=0; i<5; i++) {
[NSThread sleepForTimeInterval:.5];
NSLog(@"线程1--%d",i);
}
4万+



被折叠的 条评论
为什么被折叠?



