iOS学习笔记-116.多线程15——NSOperationQueue和自定义NSOperation合用实现多线程
2017-09-05 22:55
621 查看
多线程15NSOperationQueue和自定义NSOperation合用实现多线程
一自定义NSOperation
二NSOperationQueue和自定义NSOperation合用实现多线程
1 自定义NSOperation QWMOperation
2 代码示例
3 运行结果
三自定义NSOperation和NSThread的时候main方法说明
1 自定义 NSOperation 验证
11 QWMOperationm
12 代码示例
13 运行结果
2 自定义 QWMThread 验证
21 QWMThread
22 示例代码
23 运行结果
方法
一自定义NSOperation
二NSOperationQueue和自定义NSOperation合用实现多线程
1 自定义NSOperation QWMOperation
2 代码示例
3 运行结果
三自定义NSOperation和NSThread的时候main方法说明
1 自定义 NSOperation 验证
11 QWMOperationm
12 代码示例
13 运行结果
2 自定义 QWMThread 验证
21 QWMThread
22 示例代码
23 运行结果
多线程15——NSOperationQueue和自定义NSOperation合用实现多线程
一、自定义NSOperation
自定义NSOperation我们需要继承自 NSOperation 重写- (void)main;
方法
二、NSOperationQueue和自定义NSOperation合用实现多线程
2.1 自定义NSOperation ( QWMOperation )
// // QWMOperation.m // 03_UIview85多线程_NSOperation // // Created by 杞文明 on 17/9/4. // Copyright © 2017年 杞文明. All rights reserved. // #import "QWMOperation.h" @implementation QWMOperation //告知要执行的任务是什么 , 自定义重新main方法,任务封装到main中 //1.有利于代码隐蔽 //2.复用性 -(void)main{ [super main]; NSLog(@"----QWMOperation---main---%@",[NSThread currentThread]); } @end
2.2 代码示例
/*自定义操作 和 NSOperationQueue合用,实现多线程。自定义操作需要把任务封装到 main 方法中*/ -(void)customOperationWithQueue{ //1.创建操作 QWMOperation *op1 = [[QWMOperation alloc]init]; QWMOperation *op2 = [[QWMOperation alloc]init]; QWMOperation *op3 = [[QWMOperation alloc]init]; //2.创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //3.添加任务到队列中 [queue addOperation:op1]; [queue addOperation:op2]; [queue addOperation:op3]; }
2.3 运行结果
[15936:79761] ----QWMOperation---main---<NSThread: 0x60800007c500>{number = 4, name = (null)} [15936:79766] ----QWMOperation---main---<NSThread: 0x60000007c380>{number = 5, name = (null)} [15936:79765] ----QWMOperation---main---<NSThread: 0x60800007a340>{number = 3, name = (null)}
三、自定义NSOperation和NSThread的时候main方法说明
我们自定义NSOperation和NSThread的时候,任务的执行都是调用 start 来执行的,但是我们的任务都写在 main 中。我们猜想 我们的start方法应该是调用了 main 方法。3.1 自定义 NSOperation 验证
3.1.1 QWMOperation.m
// // QWMOperation.m // 03_UIview85多线程_NSOperation // // Created by 杞文明 on 17/9/4. // Copyright © 2017年 杞文明. All rights reserved. // #import "QWMOperation.h" @implementation QWMOperation //告知要执行的任务是什么 , 自定义重新main方法,任务封装到main中 //1.有利于代码隐蔽 //2.复用性 -(void)main{ [super main]; NSLog(@"----QWMOperation---main---%@",[NSThread currentThread]); } - (void)start{ NSLog(@"---start--start"); [super start]; NSLog(@"---start--end"); } @end
3.1.2 代码示例
/*自定义操作 和 NSOperationQueue合用,实现多线程。自定义操作需要把任务封装到 main 方法中*/ -(void)customOperationWithQueue{ //1.创建操作 QWMOperation *op1 = [[QWMOperation alloc]init]; //2.创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //3.添加任务到队列中 [queue addOperation:op1]; }
3.1.3 运行结果
[17276:85179] ---start--start [17276:85179] ----QWMOperation---main---<NSThread: 0x608000076c80>{number = 3, name = (null)} [17276:85179] ---start--end
3.2 自定义 QWMThread 验证
3.2.1 QWMThread
// // QWMThread.m // 03_UIview85多线程_NSOperation // // Created by 杞文明 on 17/9/5. // Copyright © 2017年 杞文明. All rights reserved. // #import "QWMThread.h" @implementation QWMThread -(void)main{ [super main]; NSLog(@"----QWMThread---main---%@",[NSThread currentThread]); } - (void)start{ NSLog(@"---start--start---%@",[NSThread currentThread]); [super start]; NSLog(@"---start--end---%@",[NSThread currentThread]); } @end
3.2.2 示例代码
-(void)testThread{ QWMThread *thread = [[QWMThread alloc]init]; [thread start]; }
3.2.3 运行结果
[18448:91233] ---start--start---<NSThread: 0x60800007b980>{number = 1, name = main} [18448:91233] ---start--end---<NSThread: 0x60800007b980>{number = 1, name = main} [18448:91471] ----QWMThread---main---<QWMThread: 0x600000262a00>{number = 3, name = (null)}
相关文章推荐
- iOS学习笔记-114.多线程13——NSOperationQueue和NSInvocationOperation合用实现多线程
- iOS学习笔记-115.多线程14——NSOperationQueue和NSBlockOperation合用实现多线程
- iOS学习笔记15—Cocoa:NSOperation和NSOperationQueue
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- iOS-----使用NSOperation与NSOperationQueue实现多线程
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- iOS学习笔记-117.多线程16——NSOperationQueue最大并发数
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- iOS学习笔记-118.多线程17——NSOperationQueue队列的取消、暂停、恢复
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- IOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用