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(操作队列)中去执行,而且是异步 ...
随机推荐
- android自动获取短信验证码
前言:android应用的自动化测试必然会涉及到注册登录功能,而许多的注册登录或修改密码功能常常需要输入短信验证码,因此有必要能够自动获得下发的短信验证码.主要就是实时获取短信信息.android上获 ...
- Oracle TnsName问题记录
在多次oracle服务器搭建过程中,经常遇到tnsname不正确的情况1.安装了client 这个时候XX/client/network/admin/中也有一个tnsname,而且在环境变量中,系统是 ...
- SQL Developer新建连接
1.打开SQL Developer,新建sys连接(sys为系统管理员拥有最高权限): 2.新建system连接(system为本地管理员,拥有次高权限): 3.新建scott(scott为普通用户, ...
- js中正则表达式 ---- 现成
1 . 校验密码强度 密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间. ^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 2. 校验中 ...
- 作业调度框架 Quartz 学习笔记(三) -- Cron表达式 (转载)
前面两篇说的是简单的触发器(SimpleTrigger) , SimpleTrigger 只能处理简单的事件出发,如果想灵活的进行任务的触发,就要请出 CronTrigger 这个重要人物了. Cro ...
- winform webbrowser flash显示
string flashSrc = "e:\\t.swf"; StringBuilder sb = new StringBuilder(); sb.Append("< ...
- Highcharts使用
Highcharts图表控件是使用比较广泛的图表控件.本文将介绍如何配置Highcharts和动态生成Highcharts图表.可参考Highcharts官网:http://www.highchart ...
- LAMP理论整理
关于PHP 官网:http://www.php.net 一.PHP简介 PHP是通用服务器端脚本编程语言,其主要用于web开发以实现动态web页面,它也是最早实现将脚本嵌入HTML源码文档中的服务器端 ...
- C#中IQueryable和IEnumberable的区别
IQueryable和IEnumberable的区别主要在查询方面有区别 IQueryable查询时间是先把skip和Take翻译成sql语句,去数据库执行完成后把数据加载到内存中 IEnumbera ...
- SQLServer - 约束
一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是关于列的,对于所有行,某一列有 ...