#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#warning mark nsoperation 本身不是线程,他只是封装了一定的代码段和数据去实现一个功能
- (IBAction)invocationOP:(UIButton *)sender {
NSInvocationOperation *invocation =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(text) object:nil];
[invocation start];
}
//nsblock
- (IBAction)blockOP:(UIButton *)sender {
NSBlockOperation *block =[NSBlockOperation blockOperationWithBlock:^{
[self text];
}];
[block start];
}
//线程队列(!!!!!子线程队列是无序的)
- (IBAction)opretionqu:(UIButton *)sender {
//创建一个子线程队列(并发)(不需要我们在去手动开启)
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
/* 依赖关系:(1)可保证执行顺序,(2)可以跨队列,而串行队列是不可以夸队列,如最后跟新UI则变成了在主队列朱,这是NSoperationqueue的优势
*/
// NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
// NSLog(@"下载图片%@",[NSThread currentThread]);
// }];
// NSBlockOperation *op2=[NSBlockOperation blockOperationWithBlock:^{
// NSLog(@"国栋开始P图%@",[NSThread currentThread]);
// }];
// NSBlockOperation *op3=[NSBlockOperation blockOperationWithBlock:^{
// NSLog(@"保存图片%@",[NSThread currentThread]);
// }];
//
// NSBlockOperation *op4=[NSBlockOperation blockOperationWithBlock:^{
// NSLog(@"刷新UI%@",[NSThread currentThread]);
// }];
//添加依赖关系(依赖关系要添加到添加队列之前)
// [op2 addDependency:op1];//也就是下载结束了之后再对图片进行修改,一种依赖关系;
// [op3 addDependency:op2];
// [op4 addDependency:op3];//夸队列添加依赖关系;
// //设置最大并发数,也就是说当前对列最多由几个线程同时执行,一般情况下,设置为二或三(设置为0时,相当于不执行)
// queue.maxConcurrentOperationCount =2;
// //吧op1 op2 op3 添加到队列
// [queue addOperation:op1];
// [queue addOperation:op2];
// [queue addOperation:op3];
// //把OP4添加到主线程中
// [[NSOperationQueue mainQueue] addOperation:op4];
NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1");
}];
NSBlockOperation *op2=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2");
}];
NSBlockOperation *op3 =[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3");
}];
NSBlockOperation *op4 =[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"4");
}];
NSBlockOperation *op5 =[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"5");
}];
NSBlockOperation *op6=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"6");
}];
[op2 addDependency:op1];
[op3 addDependency:op2];
[op6 addDependency:op3];
[op4 addDependency:op6];
[op5 addDependency:op4];
// [op6 addDependency:op5];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
[queue addOperation:op5];
[[NSOperationQueue mainQueue]addOperation:op6];
}
-(void)text{
NSLog(@"%@,%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);
}