#import "ViewController.h"
#import "XMGOperation.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self customOperation];
}
-(void)customOperation
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
XMGOperation *op1 = [[XMGOperation alloc]init];
XMGOperation *op2 = [[XMGOperation alloc]init];
//3.添加到队列
[queue addOperation:op1];
[queue addOperation:op2];
}
-(void)blockOperation
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1----%@",[NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2----%@",[NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3----%@",[NSThread currentThread]);
}];
[op3 addExecutionBlock:^{
NSLog(@"4----%@",[NSThread currentThread]);
}];
[op3 addExecutionBlock:^{
NSLog(@"5----%@",[NSThread currentThread]);
}];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
}
-(void)invocationOperation
{
//1.创建队列
/*
GCD:
串行(2
并发(2
NSOperation
主队列:串行[NSOperation mainQueue]
非主队列:同时具备了并发和串行的功能,默认是并发 [NSOperation alloc]init]
*/
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
//3.把操作添加到队列里面
[queue addOperation:op1]; //[op1 start]
[queue addOperation:op2];
[queue addOperation:op3];
}
-(void)download1
{
NSLog(@"download1----%@",[NSThread currentThread]);
}
-(void)download2
{
NSLog(@"download2----%@",[NSThread currentThread]);
}
-(void)download3
{
NSLog(@"download3----%@",[NSThread currentThread]);
}
@end