iOS多线程---NSOperation介绍和使用

1.  NSOperation实现多线程编程,需要和NSOperationQueue一起使用。

(1)先将要执行的操作封装到NSOperation中

(2)将NSOperation对象添加到NSOperationQueue中

(3)系统将自动将NSOPeration从NSOperationQueue中取出来,放到一个新的线程中去执行

2.NSOperation 的子类

  NSOperation是一个抽象类,并没有封装操作的能力。因此要用到它的子类:

(1)NSInvocationOperation

  (2)NSBlockOperation

3. NSInvocationOperation 和NSBlockOperation  的创建

(1)NSInvocationOperation的创建

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];

    [operation start];

2017-06-18 10:17:04.165 demo[17389:3000329] 现在的线程----<NSThread: 0x60000007bf80>{number = 1, name = main}

 注:操作对象默认在主线程中执行,只有加入到队列中才会开启新的线程。既默认情况下,如果操作没有放到Queue中只是同步执行,只有放到了NSOperationQueue中才会异步执行。

(2)NSBlockOperation的创建

(1)代码1

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"现在的线程----%@",[NSThread currentThread]);
    }];

    [operation start];

打印结果:

2017-06-18 10:20:19.440 demo[17409:3003510] 现在的线程----<NSThread: 0x6080000713c0>{number = 1, name = main}

(2)代码2

- (void)viewDidLoad {
    [super viewDidLoad];

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"现在的线程----%@",[NSThread currentThread]);
    }];

    [operation addExecutionBlock:^{
        NSLog(@"addExecution 的线程  ----%@",[NSThread currentThread]);

    }];

    [operation start];
}

2017-06-18 10:26:31.545 demo[17436:3012476] 现在的线程----<NSThread: 0x608000079900>{number = 1, name = main}

2017-06-18 10:26:31.545 demo[17436:3012571] addExecution 的线程  ----<NSThread: 0x600000262f00>{number = 3, name = (null)}

 注:NSBlockOperation 封装的操作数>1  那么就会开启新的线程。

 

4.NSOperationQueue

NSOperationQueue是操作对象的队列。将NSOperation对象放到NSOperationQueue里,系统会自动取出NSOperation,开启新线程 。

(1)NSOperationQueue添加操作的方式

    //定义队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //将操作添加到队列
    [queue addOperation:operation];
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    //用block的方式添加操作
    [queue addOperationWithBlock:^{
        NSLog(@"现在的线程----%@",[NSThread currentThread]);
    }];

(2)NSOperationQueue开启线程的方式

//
//  ViewController.m
//  demo
//
//  Created by 登 on 2017/6/17.
//  Copyright ? 2017年 登. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction1) object:nil];

    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"现在的线程----%@",[NSThread currentThread]);
    }];

    [operation2 addExecutionBlock:^{
        NSLog(@"addExecution 的线程  ----%@",[NSThread currentThread]);

    }];

    //定义队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //将操作添加到队列
    [queue addOperation:operation];
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    //用block的方式添加操作
    [queue addOperationWithBlock:^{
        NSLog(@"现在的线程----%@",[NSThread currentThread]);
    }];

}

-(void)testAction
{
    NSLog(@"现在的线程----%@",[NSThread currentThread]);
}

-(void)testAction1
{
    NSLog(@"现在的线程----%@",[NSThread currentThread]);
}

@end

2017-06-18 10:49:44.163 demo[17479:3030821] 现在的线程----<NSThread: 0x60000007c380>{number = 5, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030826] 现在的线程----<NSThread: 0x60000007c040>{number = 3, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030820] 现在的线程----<NSThread: 0x60000007c2c0>{number = 4, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030842] 现在的线程----<NSThread: 0x60000007c540>{number = 6, name = (null)}

2017-06-18 10:49:44.164 demo[17479:3030846] addExecution 的线程  ----<NSThread: 0x60800006ec40>{number = 7, name = (null)}

注:

1.NSOPerationQueue将操作取出,每一个操作都会开启一个新的线程。所有的操作都是异步并发执行。

2.其中Operation2用NSBlockOPeration添加了两个任务,这两个任务开启两个线程,同样并发执行。

时间: 2024-08-07 14:16:34

iOS多线程---NSOperation介绍和使用的相关文章

iOS开发——多线程OC篇&amp;(九)多线程NSOperation介绍

NSOperation介绍 一.NSOperation简介 1.简单说明 NSOperation的作?:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation对象添加到NSOperationQueue中 (3)系统会?动将NSOperationQueue中的NSOperation取出来 (4

iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信

一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加到NSOperationQueue中 系统会自动将NSOperationQueue中的NSOperation取出来 将取出的NSOperation封装的操作放到一条新线程中执行 NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类 使用NSOperation子类的方式有3种 N

IOS多线程--NSOperation\NSOperationQueue

iOS中,有多种多线程方式,但是,苹果公司建议我们用NSOperation技术 1.GCD是纯C的,对面向对象的程序员不友好 2.GCD对编程管理还不是很强大 NSOperation 其实底层实现是基于GCD来做的 dispatch_queue_t ==  NSOperationQueue dispatch_async == NSOperation dispatch_sync  == NSOperation NSOperation是个抽象类 使用它的两个子类 NSInvocationOperat

iOS 多线程 - NSOperation

一.简介 NSOperation 的作用 NSOperation 是对 GCD 的一层封装,更加面向对象. 配合使用 NSOperation 和 NSOperationQueue 也能实现多线程编程. NSOperation 和 NSOperationQueue 实现多线程的具体步骤 先将需要执行的操作封装到一个 NSOpertion 对象中 然后将 NSOperation 对象添加到 NSOperationQueue 中 系统会自动将 NSOperationQueue 中的 NSOperati

ios多线程 -- NSOperation 简介

NSOperation的作?:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: 1)先将需要执行的操作封装到一个NSOperation对象中 2)然后将NSOperation对象添加到NSOperationQueue中 3)系统会?动将NSOperationQueue中的NSOperation取出来 4)将取出的NSOperation封装的操作放到?条新线程中执? NSOperati

iOS多线程--NSOperation

NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象,并且比GCD多了一些更加简单实用的功能,另外,由于它的API是纯OC的,深受广大程序员喜爱,实用频率很高. NSOperation主要和NSOperationQueue配合使用实现多线程,一般步骤如下: 1.先将需要执行的操作封装到一个NSOperation对象中: 2.然后将NSOperatio

ios开发-NSOperation介绍

简介: 1.NSOperation是苹果对GCD的一个面向对象的封装,是OC的 2.NSOperation同时提供了一些GCD不是特别容易实现的功能 3.将操作添加到队列,操作会被立即”异步“执行 4.NSOperation是个抽象的类,并不具备封装操作的能力,必须使用它的子类 1>NSInvocationOperation 2>NSBlockOperation 3>自定义类继承NSOperation,实现内部的方法 代码实现: 示例1:NSInvocationOperation @in

iOS 多线程-NSOperation/NSOperationQueue

简介NSOperationQueue(操作队列)是由GCD提供的队列模型的Cocoa抽象,是一套Objective-C的APIGCD提供了更加底层的控制,而操作队列则在GCD之上实现了一些方便的功能,这些功能对于开发者而言通常是最好最安全的选择队列及操作 是面向对象的线程技术提供了一些在GCD中不容易实现的特性,如:限制最大并发数量.操作之间的依赖关系 NSOperationQueue有两种不同类型的队列:主队列和自定义队列主队列运行在主线程上自定义队列在后台执行队列处理的任务是NSOperat

iOS多线程---NSOperation的常用操作

1.最大并发数: - (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger)cnt; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; queue.maxConcurrentOperationCount = 3; 注:系统一般会根据内存大小自动设置并发数目,也可以自己设定,但是不要乱设,一般不要超过5个. 2