NSInvocationOperation,NSBlockOperation,自定义
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSInvocationOperation *iOp = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
[iOp start];
NSBlockOperation *bOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation----%@",[NSThread currentThread]);
}];
//追加任务Execution执行 当一个操作中的任务数据大于1的时候,就会开子线程并发执行任务
[bOp addExecutionBlock:^{
NSLog(@"NSBlockOperation_addExecutionBlock----%@",[NSThread currentThread]);
}];
[bOp start];
RJOperation *rjOp = [[RJOperation alloc]init];
[rjOp start];
}
-(void)download{
NSLog(@"NSInvocationOperation----%@",[NSThread currentThread]);
}
NSInvocationOperation----<NSThread: 0x60000239c980>{number = 1, name = main}
NSBlockOperation----<NSThread: 0x60000239c980>{number = 1, name = main}
NSBlockOperation_addExecutionBlock----<NSThread: 0x6000023d11c0>{number = 4, name = (null)}
2020-12-11 11:17:42.582228+0800 RJCustomTool[3797:101929] RJOperation----<NSThread: 0x60000239c980>{number = 1, name = main}
2020-12-11 11:17:46.582409+0800 RJCustomTool[3797:101929] -[RJOperation dealloc]
RJOperation
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface RJOperation : NSOperation
@end
NS_ASSUME_NONNULL_END
#import "RJOperation.h"
@implementation RJOperation
-(void)main{
NSLog(@"RJOperation----%@",[NSThread currentThread]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//4秒过后取消线程
[self cancel];
});
}
-(void)dealloc{
NSLog(@"%s",__func__);
}
@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSInvocationOperation *iOp = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
NSBlockOperation *bOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation----%@",[NSThread currentThread]);
}];
[queue addOperationWithBlock:^{
NSLog(@"addOperationWithBlock----%@",[NSThread currentThread]);
}];
RJOperation *rjOp = [[RJOperation alloc]init];
[queue addOperation:iOp];
[queue addOperation:bOp];
[queue addOperation:rjOp];
}
-(void)download{
NSLog(@"NSInvocationOperation----%@",[NSThread currentThread]);
}
addOperationWithBlock----<NSThread: 0x600003cfee00>{number = 3, name = (null)}
NSBlockOperation----<NSThread: 0x600003cfc840>{number = 4, name = (null)}
NSInvocationOperation----<NSThread: 0x600003ce5c40>{number = 9, name = (null)}
2020-12-11 11:30:27.381186+0800 RJCustomTool[3998:115346] RJOperation----<NSThread: 0x600003cfee00>{number = 3, name = (null)}
2020-12-11 11:30:31.381343+0800 RJCustomTool[3998:115106] -[RJOperation dealloc]
队列中的任务是有状态的:暂停和取消操作不能作用于当前正处于运行状态的操作
[queue setSuspended:YES];//暂停
[queue setSuspended:NO];//恢复
[queue cancelAllOperations];//取消
/*
设置最大并发数量
maxConcurrentOperationCount 最大并发数:同一时间最多可以处理的任务数量
队列默认是并发队列
maxConcurrentOperationCount == 1 串行队列
-1 在计算机 中表示的是最大值(默认是不受限制的)*/
queue.maxConcurrentOperationCount = -1;
不能设置循环依赖(死锁-不会执行)
[iOp addDependency:rjOp];
[rjOp addDependency:bOp];
NSBlockOperation----<NSThread: 0x600000870cc0>{number = 7, name = (null)}
2020-12-11 11:36:44.313473+0800 RJCustomTool[4085:121948] RJOperation----<NSThread: 0x600000870cc0>{number = 7, name = (null)}
NSInvocationOperation----<NSThread: 0x600000870cc0>{number = 7, name = (null)}
2020-12-11 11:36:48.313641+0800 RJCustomTool[4085:121654] -[RJOperation dealloc]
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
NSLog(@"OperationQueue----%@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"UI----%@",[NSThread currentThread]);
}];
}];
OperationQueue----<NSThread: 0x60000204afc0>{number = 7, name = (null)}
UI----<NSThread: 0x600002024a40>{number = 1, name = main}
``
``
``
``
``
``
网友评论