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添加了两个任务,这两个任务开启两个线程,同样并发执行。
iOS多线程---NSOperation介绍和使用的更多相关文章
- iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信
一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加 ...
- iOS 多线程 NSOperation、NSOperationQueue
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...
- iOS多线程 NSOperation的用法
上一篇写了 GCD 的使用,接下来就了解一下 NSOperation ,NSOperation是苹果对 GCD 的 OC 版的一个封装,但是相对于GCD来说可控性更强,并且可以加入操作依赖. NSOp ...
- iOS多线程--NSOperation 浅显易懂
NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...
- iOS多线程--NSOperation
NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...
- IOS 多线程 NSOperation GCD
1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...
- iOS多线程---NSOperation的常用操作
1.最大并发数: - (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger) ...
- iOS多线程编程
废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...
- [转] iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首 ...
随机推荐
- 函数数组demo
#include <stdio.h> #include <string.h> typedef int(*service_func)(char *,char *); struct ...
- 如何修改路由器的登录IP地址?
如何修改路由器的登录IP地址? 因为有多个路由器,为了区分不同路由器,我们可以修改它的登录IP,而且修改后,可以在连接的电脑上直观地知道所连接的是哪一台路由器 买回来的路由器,一般默认的登录地址是19 ...
- 【Maven】安装及配置(Linux)
本文介绍Linux环境下安装Maven 安装环境和软件 系统:Linux(CentOS) 软件:apache-maven-3.3.9-bin.tar.gz(解压版). 安装步骤 maven是基于Jav ...
- 利用ajaxSubmit()方法实现Form提交表单后回调
1. 背景 最近在工作中,需要实现网页端图片上传到FTP服务器的功能.上传文件是用Form表单提交数据的方法向后台传输文件流,在此遇到了一个问题:后台在处理完图片上传功能后,需要向前台回传是 ...
- Django(4)
https://www.cnblogs.com/yuanchenqi/articles/7439088.html
- arduino IO口
AVR单片机的每组I/O口都配备有三个8位寄存器,分别是:方向控制寄存器DDRx.数据寄存器PORTx.输入引脚寄存器PINx(x=A/B/C/D).I/O口的工作方式和表现特征由这三个I/O寄存器控 ...
- The class cn.itcast.web.common.util.UtilFuns specified in TLD for the function selffn:htmlNewline cannot be found: cn.itcast.web.common.util.UtilFuns
我的一个Util方法的包名更改了,运行时候报这个错误.找到tld文件,把包名重新改为我改的名字就好使了.
- 自学如何去学习jQuery
学习JQ第一个demo: 制作一个轮播图,制作方法我前面写了一篇博客,传送门-->http://www.cnblogs.com/yewenxiang/p/6100206.html 需要的JQ知识 ...
- tornado+bootstrap急速搭建你自己的网站
bootstrap既然是这么的流行又能省很多的事为什么不用他呢?再加上牛X的produced by FB的tornado简直如虎添翼了! 1. 安装配置 安装所需要的库等内容.这里没什么需要多讲的.t ...
- C语言printf的格式
例1 int a = 12345;printf("%6d",a); // 输出6位不够左边补空格printf("%.6d",a); // 输出6位不够左边补0例 ...