博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈多线程——NSOperation
阅读量:5020 次
发布时间:2019-06-12

本文共 11102 字,大约阅读时间需要 37 分钟。

  iOS中多线程相关方法——NSOperation及其相关类:

  NSOperation

  NSBlockOperation

  NSInvocationOperation

  NSOperationQueue

  1.NSOperation:一般在使用NSOperation进行线程操作时,不常用该类,而是用他的子类或NSOperationQueue;当然,也可以使用他的block属性创建线程。

1 @interface ViewController () 2 { 3 @public 4     int count; 5 } 6 @end 7  8 @implementation ViewController 9 - (void)viewDidLoad {10     [super viewDidLoad];11     12     NSLog(@"==Start==");13     14     count = 0;15     for (int i = 0; i < 10; i++) {16         NSOperation *opration = [[NSOperation alloc] init];17         opration.completionBlock = ^(){18             NSLog(@"%@ = %@", @"operation_block", [NSThread currentThread]);19         };20         // 开启多线程21         [opration start];22     }23     24     NSLog(@"==End==");25 }26 27 - (void)action:sender{28     count ++;29     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);30 }31 @end    2016-04-18 10:10:42.563 Demo_NSOperation[1461:47803] ==Start==                                                              2016-04-18 10:10:42.564 Demo_NSOperation[1461:47803] ==End==                                                                2016-04-18 10:10:42.564 Demo_NSOperation[1461:47978] operation_block = 
{number = 2, name = (null)} 2016-04-18 10:10:42.564 Demo_NSOperation[1461:47979] operation_block =
{number = 3, name = (null)} 2016-04-18 10:10:42.564 Demo_NSOperation[1461:47982] operation_block =
{number = 5, name = (null)} 2016-04-18 10:10:42.564 Demo_NSOperation[1461:47984] operation_block =
{number = 4, name = (null)} 2016-04-18 10:10:42.565 Demo_NSOperation[1461:47978] operation_block =
{number = 2, name = (null)} 2016-04-18 10:10:42.565 Demo_NSOperation[1461:47979] operation_block =
{number = 3, name = (null)} 2016-04-18 10:10:42.566 Demo_NSOperation[1461:47985] operation_block =
{number = 6, name = (null)} 2016-04-18 10:10:42.566 Demo_NSOperation[1461:47982] operation_block =
{number = 5, name = (null)} 2016-04-18 10:10:42.566 Demo_NSOperation[1461:47984] operation_block =
{number = 4, name = (null)} 2016-04-18 10:10:42.567 Demo_NSOperation[1461:47978] operation_block =
{number = 2, name = (null)}

 

 

  2.NSBlockOperation:是NSOperation的子类

1 @interface ViewController () 2 { 3     @public 4     int count; 5 } 6 @end 7  8 @implementation ViewController 9 10 - (void)viewDidLoad {11     [super viewDidLoad];12     13     NSLog(@"==Start==");14     15     count = 0;16     for (int i = 0; i < 10; i++) { // 直接使用NSBlockOperation的类方法    17         NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{18             NSLog(@"%@ = %@", @"blockOperation", [NSThread currentThread]);19         }];20         21         [blockOperation start];22     }23     24     NSLog(@"==End==");25 }26 27 - (void)action:sender{28     count ++;29     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);30 }31 @end 2016-04-18 12:04:03.893 Demo_NSOperation[2103:104349] ==Start==                                                           2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = 
{number = 1, name = main} 2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation =
{number = 1, name = main} 2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] ==End==
1 @interface ViewController () 2 { 3     @public 4     int count; 5 } 6 @end 7  8 @implementation ViewController 9 - (void)viewDidLoad {10     [super viewDidLoad];11     12     NSLog(@"==Start==");13     14     count = 0;15     for (int i = 0; i < 5; i++) {16         NSBlockOperation *blockOperation = [NSBlockOperation new];17         // 使用NSBlockOperation的实例方法18         [blockOperation addExecutionBlock:^{19             NSLog(@"%@ = %@", @"blockOperation1", [NSThread currentThread]);20         }];21         22         [blockOperation addExecutionBlock:^{23             NSLog(@"%@ = %@", @"blockOperation2", [NSThread currentThread]);24         }];25         26         [blockOperation addExecutionBlock:^{27             NSLog(@"%@ = %@", @"blockOperation3", [NSThread currentThread]);28         }];29         30         [blockOperation start];31     }32     33     NSLog(@"==End==");34 }35 36 - (void)action:sender{37     count ++;38     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);39 }40 @end 2016-04-18 14:07:23.518 Demo_NSOperation[2797:178037] ==Start==                                                              2016-04-18 14:07:23.520 Demo_NSOperation[2797:178037] blockOperation1 = 
{number = 1, name = main} 2016-04-18 14:07:23.520 Demo_NSOperation[2797:178070] blockOperation3 =
{number = 2, name = (null)} 2016-04-18 14:07:23.520 Demo_NSOperation[2797:178069] blockOperation2 =
{number = 3, name = (null)} 2016-04-18 14:07:23.521 Demo_NSOperation[2797:178037] blockOperation1 =
{number = 1, name = main} 2016-04-18 14:07:23.521 Demo_NSOperation[2797:178070] blockOperation3 =
{number = 2, name = (null)} 2016-04-18 14:07:23.521 Demo_NSOperation[2797:178069] blockOperation2 =
{number = 3, name = (null)} 2016-04-18 14:07:23.522 Demo_NSOperation[2797:178037] blockOperation1 =
{number = 1, name = main} 2016-04-18 14:07:23.522 Demo_NSOperation[2797:178070] blockOperation3 =
{number = 2, name = (null)} 2016-04-18 14:07:23.522 Demo_NSOperation[2797:178069] blockOperation2 =
{number = 3, name = (null)} 2016-04-18 14:07:23.523 Demo_NSOperation[2797:178037] blockOperation1 =
{number = 1, name = main} 2016-04-18 14:07:23.523 Demo_NSOperation[2797:178070] blockOperation2 =
{number = 2, name = (null)} 2016-04-18 14:07:23.523 Demo_NSOperation[2797:178069] blockOperation3 =
{number = 3, name = (null)} 2016-04-18 14:07:23.524 Demo_NSOperation[2797:178037] blockOperation1 =
{number = 1, name = main} 2016-04-18 14:07:23.524 Demo_NSOperation[2797:178070] blockOperation3 =
{number = 2, name = (null)} 2016-04-18 14:07:23.524 Demo_NSOperation[2797:178069] blockOperation2 =
{number = 3, name = (null)} 2016-04-18 14:07:23.524 Demo_NSOperation[2797:178037] ==End==

 

  3.NSInvocationOperation:是NSOperation的子类

1 @interface ViewController () 2 { 3     @public 4     int count; 5 } 6 @end 7  8 @implementation ViewController 9 - (void)viewDidLoad {10     [super viewDidLoad];11 12     NSLog(@"==Start==");13     14     count = 0;15     for (int i = 0; i < 5; i++) {16         // 使用构造方法17         NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"invocationOperation"];18         // 使用实例方法19         [invocationOperation setCompletionBlock:^{20             NSLog(@"%@ %@", @"iop", [NSThread currentThread]);21         }];22         23         [invocationOperation start];24     }25     26     NSLog(@"==End==");27 }28 29 - (void)action:sender{30     count ++;31     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);32 }33 @end 2016-04-18 14:18:41.245 Demo_NSOperation[2929:183996] ==Start==                                                                   2016-04-18 14:18:41.246 Demo_NSOperation[2929:183996] invocationOperation[1] = 
{number = 1, name = main} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[2] =
{number = 1, name = main} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[3] =
{number = 1, name = main} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[4] =
{number = 1, name = main} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:184167] iop
{number = 2, name = (null)} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[5] =
{number = 1, name = main} 2016-04-18 14:18:41.248 Demo_NSOperation[2929:183996] ==End== 2016-04-18 14:18:41.248 Demo_NSOperation[2929:184170] iop
{number = 4, name = (null)} 2016-04-18 14:18:41.247 Demo_NSOperation[2929:184171] iop
{number = 3, name = (null)} 2016-04-18 14:18:41.248 Demo_NSOperation[2929:184167] iop
{number = 2, name = (null)} 2016-04-18 14:18:41.248 Demo_NSOperation[2929:184177] iop
{number = 5, name = (null)}

  无论是NSBlockOperation的类方法还是NSInvocationOperation实例方法都在主线程中执行;而使用addExecutionBlock方法则会在主线程及子线程中运行;使用setCompletionBlock方法则会开辟多线程。

 

  4.NSOperationQueue:并不是NSOperation的子类,但是常与NSOperation搭配使用,加入queue的操作会开辟子线程处理

1 @interface ViewController () 2 { 3     @public 4     int count; 5 } 6 @end 7  8 @implementation ViewController 9 10 - (void)viewDidLoad {11     [super viewDidLoad];12 13     NSLog(@"==Start==");14     15     count = 0;16     NSOperationQueue *queue = [[NSOperationQueue alloc] init];17     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue"];18     NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue1"];19     NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue2"];20     [queue addOperations:@[operation, operation1, operation2] waitUntilFinished:YES];21     22     NSLog(@"==End==");23 }24 25 - (void)action:sender{26     count ++;27     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);28 }29 @end 2016-04-18 15:03:12.566 Demo_NSOperation[3259:201876] ==Start==                                                 2016-04-18 15:03:12.567 Demo_NSOperation[3259:202092] operationQueue2[2] = 
{number = 4, name = (null)} 2016-04-18 15:03:12.567 Demo_NSOperation[3259:202072] operationQueue[3] =
{number = 3, name = (null)} 2016-04-18 15:03:12.567 Demo_NSOperation[3259:202070] operationQueue1[1] =
{number = 2, name = (null)} 2016-04-18 15:03:12.568 Demo_NSOperation[3259:201876] ==End==

  NSOperation中含有@property NSOperationQueuePriority queuePriority;@property NSQualityOfService qualityOfService;的枚举属性,可选择线程的优先级以及子线程开辟方式。

 

转载于:https://www.cnblogs.com/kriskee/p/5404564.html

你可能感兴趣的文章
7、shell函数
查看>>
【转】Apache Jmeter发送post请求
查看>>
Nginx 基本 安装..
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
在没装VS2010的机器上运行VS2010开发的C++程序
查看>>
[Oracle Notes]About Oracle parallel insert performance-有关oracle并行插入性能
查看>>
[Luogu 2805] NOI2009 植物大战僵尸
查看>>
【Python】多线程2
查看>>
【spark】with mongodb
查看>>
矩阵取数游戏
查看>>
php 操作数组 (合并,拆分,追加,查找,删除等)
查看>>
Java 从无类型参数Map到有类型参数Map传值的一个问题
查看>>
css常用的阴影
查看>>
linux上安装mysql
查看>>
树上倍增+差分
查看>>
【HDU 2594 Simpsons' Hidden Talents】
查看>>
Vue2.0+组件库总结
查看>>
linux 学习随笔-shell简单编写
查看>>
万维网
查看>>
CentOS中对ext4文件系统做磁盘配额
查看>>