iOS学习笔记-116.多线程15——NSOperationQueue和自定义NSOperation合用实现多线程

多线程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)}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值