线程9--NSOperation
一、NSOperation简介
1.简单说明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
(1)先将需要执行的操作封装到一个NSOperation对象中
(2)然后将NSOperation对象添加到NSOperationQueue中
(3)系统会⾃动将NSOperationQueue中的NSOperation取出来
(4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法
/*************************************************************/
//即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作
NSLog(@"主线程---%@",[NSThread currentThread]);
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
[operation1 start];
/*************************************************************/
//注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation1----%@",[NSThread currentThread]);}
}]; [operation2 addExecutionBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation2----%@",[NSThread currentThread]);}
}];
[operation2 addExecutionBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation3----%@",[NSThread currentThread]); }
}];
//放入队列后就不需要这句;
[operation2 start]; /*************************************************************/
/*1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation对象添加到NSOperationQueue中 (3)系统会⾃动将NSOperationQueue中的NSOperation取出来 (4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
*/ NSInvocationOperation *operation3=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test3) object:nil];
NSInvocationOperation *operation4=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test4) object:nil]; NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[queue addOperation:operation2];
[queue addOperation:operation3];
[queue addOperation:operation4];
/*************************************************************/
-(void)test1{
NSLog(@"operation1--test1---%@",[NSThread currentThread]);
}
-(void)test3{
for (int i=; i<; i++) {
NSLog(@"NSOperationQueue-test3----%@",[NSThread currentThread]);
}
}
-(void)test4{
for (int i=; i<; i++) {
NSLog(@"NSOperationQueue--test4---%@",[NSThread currentThread]);
}
}
线程9--NSOperation的更多相关文章
- NSThread 子线程 Cocoa NSOperation GCD(Grand Central Dispatch) 多线程
单词:thread 英 θred:n 线.思路.vt 穿过.vi 穿透过 一. 进程.线程 进程:正在进行中的程序被称为进程,负责程序运行的内存分配,每一个进程都有自己独立的虚拟内存空间 线程: ...
- 创建线程方式-NSOperation
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 线程2 NSOperation 抽像类的使用
// // ZYOperationViewController.h // Thread // // Created by yejiong on 14 // // ZYOperation.h // Th ...
- IOS-多线程(NSOperation)
一.基础用法 // // ViewController.m // IOS_0120_NSOperation // // Created by ma c on 16/1/20. // Copyright ...
- 4.4 多线程进阶篇<下>(NSOperation)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人"简书" 本文源码 Demo 详见 Github https://github.c ...
- NSOperation、NSOperationQueue(II)
NSOperationQueue 控制串行执行.并发执行 NSOperationQueue 创建的自定义队列同时具有串行.并发功能 这里有个关键属性 maxConcurrentOperationCou ...
- iOS线程开发小结
在iOS开发线程操作时,一般方法名决定是否开启新线程(async,sync),队列类型(全局队列,串行队列)决定开启多少条线程 1.快速线程调用 *开启后台线程执行任务 [self performSe ...
- iOS线程之——NSCondition
多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美.这篇文章主要从线程创建与启动.线程的同步与锁.线程的交互.线 ...
- iOS:操作队列实现多线程NSOperation
NSOperation具体使用:直接继承NSObject 它的子类有:NSBlockOperation.NSInvocationOperation 还有一个必须的类,队列,用来装创建的线程 NSOpe ...
- iOS线程浅析
一.线程概述 1. iOS里面的线程按种类可分为同步线程和异步线程.同步线程指调用同步线程的地方必须等到同步线程运行完成才干够继续向下运行.而调用异步线程的地方则在运行完调用异步线程的语句后就能够继续 ...
随机推荐
- [原创] 基础中的基础(二):C/C++ 中 const 修饰符用法总结
在这篇文章中,我总结了一些C/C++语言中的 const 修饰符的常见用法,供大家参考. const 的用法,也是技术性面试中常见的基础问题,希望能够帮大家梳理一下知识,给大家一点点帮助.作者是菜鸟一 ...
- STL在数组算法的使用
find(a:起始位置 , b: 终止位置 , c: 要查找的内容) ------>查找寻找内容的位置 count(a:起始位置 , b: 终止位置 , c: 要查找的内容) -- ...
- MiniUi中ComboBox与Autocomplete的区别
ComboBox 下拉选择框与Autocomplete的区别: 1,ComboBox只在页面加载时加载一次,发送一次请求: 2,Autocomplete会根据用户输入的值动态的去发送请求加载数据:
- 【洛谷p3994】Highway 二分+斜率优化DP
题目大意:给你一颗$n$个点的有根树,相邻两个点之间有距离,我们可以从$x$乘车到$x$的祖先,费用为$dis\times P[x]+Q[x]$,问你除根以外每个点到根的最小花费. 数据范围:$n≤1 ...
- Machine learning 第8周编程作业 K-means and PCA
1.findClosestCentroids function idx = findClosestCentroids(X, centroids) %FINDCLOSESTCENTROIDS compu ...
- 八、Linux上常用网络操作
1. 主机名配置 hostname 查看主机名 hostname xxx 修改主机名 重启后无效 如果想要永久生效,可以修改/etc/sysconfig/network文件 2. IP地址配置 set ...
- 如何在window server IIS上部署可以使用web deploy?
环境: windows server2012 方式1: 1,下载"wpilauncher.exe" Web平台安装程序.下载地址:http://www.microsoft.com/ ...
- CentOS7 yum方式安装 MongoDB 3.4 复制集
CentOS7 yum方式安装 MongoDB 3.4 环境.准备 Centos7 系统 配置MongoDB的yum源,添加文件/etc/yum.repos.d/mongodb-org-3.4.rep ...
- log4net udp
官方文档: http://logging.apache.org/log4net/release/config-examples.html 配置: <?xml version="1.0& ...
- 【树】Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...