线程10--NSOperation的基本操作
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
(1)取消队列的所有操作
- (void)cancelAllOperations;
提⽰:也可以调用NSOperation的- (void)cancel⽅法取消单个操作
(2)暂停和恢复队列
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列
- (BOOL)isSuspended; //当前状态
(1)设置NSOperation在queue中的优先级,可以改变操作的执⾏优先级
- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;
(2)优先级的取值
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8
说明:优先级高的任务,调用的几率会更大。
(1)NSOperation之间可以设置依赖来保证执行顺序,⽐如一定要让操作A执行完后,才能执行操作B,可以像下面这么写
[operationB addDependency:operationA]; // 操作B依赖于操作
(2)可以在不同queue的NSOperation之间创建依赖关系
- (void)viewDidLoad {
[super viewDidLoad];
/***********************************************************/
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:self]; NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil]; NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
for(int i=;i<;i++){
NSLog(@"NSBlockOperation--1--%@",[NSThread currentThread]);
}
}]; [operation3 addExecutionBlock:^{
for (int i=; i<; i++) {
NSLog(@"NSBlockOperation--2--%@",[NSThread currentThread]);
}
}]; //设置操作依赖
//先执行operation2,再执行operation1,最后执行operation3
[operation3 addDependency:operation1];
[operation1 addDependency:operation2]; //不能是相互依赖
// [operation3 addDependency:operation1];
// [operation1 addDependency:operation3];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
/*****************************监听**************************/
//创建对象,封装操作
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
for (int i=; i<; i++) {
NSLog(@"-operation-下载图片-%@",[NSThread currentThread]);
}
}]; //监听操作的执行完毕
operation.completionBlock=^{
//.....下载图片后继续进行的操作
NSLog(@"--接着下载第二张图片--");
}; //创建队列
NSOperationQueue *queue1=[[NSOperationQueue alloc]init];
//把任务添加到队列中(自动执行,自动开线程)
[queue1 addOperation:operation];
/***********************************************************/
}
-(void)test1{
NSLog(@"NSInvocationOperation-test1-%@",[NSThread currentThread]);
}
-(void)test2{
NSLog(@"NSInvocationOperation-test2-%@",[NSThread currentThread]); }
线程10--NSOperation的基本操作的更多相关文章
- laravel基础课程---10、数据库基本操作(如何使用数据库)
laravel基础课程---10.数据库基本操作(如何使用数据库) 一.总结 一句话总结: 1.链接数据库:.env环境配置里面 2.执行数据库操作:DB::table('users')->up ...
- iOS开发多线程篇 10 —NSOperation基本操作
iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. ...
- 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的基本操作(转载)
一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperat ...
- IOS-多线程(NSOperation)
一.基础用法 // // ViewController.m // IOS_0120_NSOperation // // Created by ma c on 16/1/20. // Copyright ...
- jedis使用线程池封装redis基本操作
redisclient jedis 经常使用的 操作 key value hash list set zset 的基本操作 package cn.zto.util; import java.util. ...
- [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 3.多线程NSOperation
1.NSOperation的基本操作 使用NSOperation的两个子类,NSInvocationOperation 和 NSBlockOperation 创建操作,然后将操作添加到队列中去执行 / ...
随机推荐
- Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual
monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...
- UWP开发---DIY星级评分控件
一,需求来源 在开发韩剧TV UWP过程中,遇到了星级评分的控件问题,在安卓和html中很容易用现有的轮子实现星级评分,搜索了一下目前UWP还未有相关文章,在WPF的一篇文章中使用Photo shop ...
- C++实现排序算法
稳定性:快速 希尔 选择 堆排序不稳定 时间复杂度:平均情况下,快速.希尔.归并和堆排序的时间复杂度均为O(nlog2(n)),其他都是O(n^2).最坏情况下,快排的时间复杂度为O(n^2) #in ...
- PropertyPlaceHolderConfigurer中的location是不是用错了?
本文由作者张远道授权网易云社区发布. spring中常用PropertyPlaceHolderConfigurer来读取properties配置文件的配置信息.常用的配置方式有两种,一种是使用loca ...
- Statement与PreparedStatement区别
1.性能区别 Statement statement = conn.createStatement(); PreparedStatement preStatement = conn.prepareSt ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 【文文殿下】ExBSGS
无需逆元版本: #include<cstdio> #include<cassert> #include<cmath> #include<map> typ ...
- python del 方法的使用
在Python 的自带函数中 del 函数是一个非常特殊但是又非常使用的函数 my_list = [1,2,3] my_dict = {"name":"lowman&qu ...
- 应该怎么理解 app = Flask(__name__)
初始化生成一个app对象,这个对象就是Flask的当前实例对象,后面的各个方法调用都是这个实例Flask会进行一系列自己的初始化,比如web API路径初始化,web资源加载,日志模块创建等.然后返回 ...
- python求100以内素数
python求100以内素数之和 from math import sqrt # 使用isPrime函数 def isPrime(n): if n <= 1: return False for ...