NSOperationQueue的基本使用
- NSOperationQueue的作用
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;
-(void)blockOperation
{
//1.创建队列
/*
并发:全局并发队列,自己创建(concurrent)
串行:主队列,自己创建(serial)
*/ // NSOperationQueue
/*
主队列:凡是放到主队列里面的任务都在主线程执行[NSOperationQueue mainQueue]
非主队列:alloc int,同时具备了并发和串行的功能,默认是并发队列
*/ NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //2.封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1----%@",[NSThread currentThread]);
}]; NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2----%@",[NSThread currentThread]);
}]; NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3----%@",[NSThread currentThread]);
}]; [op3 addExecutionBlock:^{
NSLog(@"4----%@",[NSThread currentThread]);
}]; [op3 addExecutionBlock:^{
NSLog(@"5----%@",[NSThread currentThread]);
}]; [op3 addExecutionBlock:^{
NSLog(@"6----%@",[NSThread currentThread]);
}]; //3.添加操作到队列
[queue addOperation:op1]; //[op1 start]
[queue addOperation:op2];
[queue addOperation:op3]; //简便方法
[queue addOperationWithBlock:^{
NSLog(@"7----%@",[NSThread currentThread]);
}]; [queue addOperationWithBlock:^{
NSLog(@"8----%@",[NSThread currentThread]);
}]; }
-(void)invocation
{
//1.创建队列
/*
并发:全局并发队列,自己创建(concurrent)
串行:主队列,自己创建(serial)
*/ // NSOperationQueue
/*
主队列:凡是放到主队列里面的任务都在主线程执行[NSOperationQueue mainQueue]
非主队列:alloc int,同时具备了并发和串行的功能,默认是并发队列
*/ NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //2.封装操作
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil]; NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil]; //3.添加操作到队列
[queue addOperation:op1]; //[op1 start]
[queue addOperation:op2];
[queue addOperation:op3];
} -(void)download1
{
NSLog(@"download1---%@",[NSThread currentThread]);
} -(void)download2
{
NSLog(@"download2---%@",[NSThread currentThread]);
} -(void)download3
{
NSLog(@"download3---%@",[NSThread currentThread]);
}
示例
把继承自NSOperation的类添加到Queue中:
-(void)custom
{
//1.创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //2.封装操作
YSOperation *op1 = [[YSOperation alloc]init];
YSOperation *op2 = [[YSOperation alloc]init]; //3.添加操作到队列
[queue addOperation:op1]; //[op1 start]
[queue addOperation:op2];
}
示例
NSOperationQueue的基本使用的更多相关文章
- Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全 (转)
目前在 iOS 和 OS X 中有两套先进的同步 API 可供我们使用:NSOperation 和 GCD .其中 GCD 是基于 C 的底层的 API ,而 NSOperation 则是 GCD 实 ...
- 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...
- NSOperationQueue的其他方法
1.设置最大并发数 什么是并发数 同时执行的任务数 比如,同时开3个线程执行3个任务,并发数就是3 最大并发数的相关方法 - (NSInteger)maxConcurrentOperationCo ...
- NSOperationQueue与GCD的使用原则和场景
首先,我们要明确NSOperationQueue与GCD之间的关系: NSOpertaionQueue用GCD构建封装的,是GCD的高级抽象. 其次,我们要区别两者的不同: GCD仅仅支持FIFO队列 ...
- 伟大的GCD和NSOperationQueue
一. GCD GCD中最重要的两个东西 任务 和 队列 任务就是一段代码(用来缓存,下载,计算等操作) 队列从大的方面分为两个队列:主队列(串行队列)和 自己创建的队列(串行,和并行) 主队列中: 在 ...
- 多线程NSInvocationOperation(NSOperationQueue)的基本用法
#import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...
- IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...
- GCD与NSOperationQueue
1> GCD是纯C语言的API,NSOperationQueue是基于GCD的OC版本封装 2> GCD只支持FIFO(先入先出)的队列,NSOperationQueue可以很方便地调整执 ...
- 多线程编程4 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
随机推荐
- Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy
SQLAlchemy SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language rele ...
- 仿W8屏保
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jqueryUI 插件
1,拖拽插件 draggable 拖拽插件draggable的功能是拖动被绑定的元素, 当这个jqueryUI插件与元素绑定后,可以通过调用draggable()方法,实现何种拖拽元素的效果 $(s ...
- css中左侧固定,右侧自适应
谈谈我开始出来工作时候的一道面试题吧 当初我记得在太平洋网络面试的时候,面试官给我出了这么一道题: 有一个外层的div 中间有左右两个div 要求左侧的div 1.只告诉你宽度; 2.只告 ...
- C# List泛型集合中的GroupBy<>用法
//根据子项目id得到flowjump实体类 flowJumps = this.FlowJumps; //按工序groupby flowjumps IEnumerable<IGrouping&l ...
- MVC5+EF6 入门完整教程六
本篇我们谈谈分部视图(Partial View). 上篇文章提到过Partial和Action这两个helper, 本篇文章主要就结合这两个helper来讲解分部视图(Partial View)的应用 ...
- Mongodb 副本集分片(一)---初始化mongodb安装启动
写在前面:mongodb是nosql非关系型数据库中,比较受欢迎的产品.在数据持久化及与关系型数据库的关联上也做的比较好,目前各大公司在存放二进制文件(图片.视频等)中应用也比较广泛.其遵循的key- ...
- 基于bootstrap的后台二级垂直菜单[转]
最近做一个后台的管理项目,用到了Twitter推出的bootstrap前端开发工具包,是一个基于css3/html5的框架.花周末时间,写了一个非常简单后台的菜单.本着开源的精神,现在把它分享出来(呵 ...
- 学习docker
虚拟机下Ubuntu环境 1.sudo apt-get update 2.sudo apt-get install docker.io 3.在daocloud(http://www.daocloud. ...
- 【工具】【电子设计】超屌的 fritzing 新建元件
fritzing 有多好,用了才知道,但是通常会遇到一个问题,他的元件库不一定够用,这时候就得自己做元件了,但是搜了一下网上没有相关的教程啊. 算了,去官网看英文吧.. 首先在最新版本不支持直接新建元 ...