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添加了两个任务,这两个任务开启两个线程,同样并发执行。

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

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

    一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加 ...

  2. iOS 多线程 NSOperation、NSOperationQueue

    1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...

  3. iOS多线程 NSOperation的用法

    上一篇写了 GCD 的使用,接下来就了解一下 NSOperation ,NSOperation是苹果对 GCD 的 OC 版的一个封装,但是相对于GCD来说可控性更强,并且可以加入操作依赖. NSOp ...

  4. iOS多线程--NSOperation 浅显易懂

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  5. iOS多线程--NSOperation

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  6. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...

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

    1.最大并发数: - (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger) ...

  8. iOS多线程编程

    废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...

  9. [转] iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用

    介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首 ...

随机推荐

  1. 2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MB Description Byteasar the garde ...

  2. if_elseif

    用MATLAB写了个这样的程序 if ((0 < pwr <=2) ) wf_temp1 = round(temp_wf0/2^7); elseif( (2 < pwr<= 4 ...

  3. webuploader 文件上传插件 IE8/9 文件选择不上传

    IE8/9下文件上传是采用flash模式,一直发送http://xxx.xxx.xx.xx:8888/crossdomain.xml请求,状态码为404,原因是上传文件的服务器未配置crossdoma ...

  4. web.xml 404 500 配置

    web.xml <error-page> <error-code>404</error-code> <location>/error404.html&l ...

  5. go指针的一个小坑

    几乎可以肯定的说,go语言中除了闭包在引用外部变量的时候是传引用的,其他的时候都是传值的.如果你说形参可以定义为指针.好吧,那么告诉你这个指针的值其实是按照传值的方式使用的. 下面看个很浅显的例子: ...

  6. 二:nodejs+express+redis+bootstrap table+jquery UI

    介绍:做一个量化投资的实时系统. 综合: 添加记录,顺序改变的话,refresh之后,能正常刷新吗?可以正常刷新,只是顺序又变回去. express中用fs readfile 时,需要用path.jo ...

  7. jquery使用ajax报错[Uncaught SyntaxError: Unexpected token :]

    $.post('/ajax/validate.do',{"id": id},function(ret){ //ret }); 返回值明明是json,格式也是正确的,却解析不成功,在 ...

  8. MySQL Route负载均衡与读写分离Docker环境使用

    Docker环境描述 主机名 部署服务 备注 MySQL Route MySQL Route 部署在宿主机上的MySQL Route服务 MySQL Master1 MySQL 5.7.16 Dock ...

  9. Linux 防火墙iptables命令详解

    [转:原文链接] iptables -Fiptables -Xiptables -F -t mangleiptables -t mangle -Xiptables -F -t natiptables ...

  10. C#DataGridView的简单使用

    首先创建一个DataGridView控件,然后创建列(包括列名的定义), 由于我不是和数据库进行连接,只是为了输出好看一点. 删除所有数据: while (this.dataGridView1.Row ...