• NSOperationQueue的作用
    NSOperation可以调用start方法来执行任务,但默认是同步执行的
    如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
  
    添加操作到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的基本使用的更多相关文章

  1. Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全 (转)

    目前在 iOS 和 OS X 中有两套先进的同步 API 可供我们使用:NSOperation 和 GCD .其中 GCD 是基于 C 的底层的 API ,而 NSOperation 则是 GCD 实 ...

  2. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...

  3. NSOperationQueue的其他方法

    1.设置最大并发数 什么是并发数 同时执行的任务数 比如,同时开3个线程执行3个任务,并发数就是3   最大并发数的相关方法 - (NSInteger)maxConcurrentOperationCo ...

  4. NSOperationQueue与GCD的使用原则和场景

    首先,我们要明确NSOperationQueue与GCD之间的关系: NSOpertaionQueue用GCD构建封装的,是GCD的高级抽象. 其次,我们要区别两者的不同: GCD仅仅支持FIFO队列 ...

  5. 伟大的GCD和NSOperationQueue

    一. GCD GCD中最重要的两个东西 任务 和 队列 任务就是一段代码(用来缓存,下载,计算等操作) 队列从大的方面分为两个队列:主队列(串行队列)和 自己创建的队列(串行,和并行) 主队列中: 在 ...

  6. 多线程NSInvocationOperation(NSOperationQueue)的基本用法

        #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  7. IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop

    注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...

  8. GCD与NSOperationQueue

    1> GCD是纯C语言的API,NSOperationQueue是基于GCD的OC版本封装 2> GCD只支持FIFO(先入先出)的队列,NSOperationQueue可以很方便地调整执 ...

  9. 多线程编程4 - NSOperationQueue

    一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...

随机推荐

  1. Web应用安全十大主动安全措施

    这几天有空参加了360主办的第一届互联网安全大会(ISC 2013),除了现场看到FREEBUF现场微博号发的众多妹子之外,也听到了很多有趣的议题,比如TK分享的通过网络运营商缓存服务器偷渡,只需一台 ...

  2. Python:list用法

    list是一种有序的集合,可以随时添加和删除其中的元素. 定义 空list >>> a_list=[] >>> a_list [] 普通 >>> ...

  3. C# 模拟webform里面按钮的点击事件

    生成的html内容 <body> <form method="post" action="./Login.aspx" id="for ...

  4. 记一次酷派尚锋Y75刷机

    昨天因为手机卡的原因,我的同学帮他的同学刷机,听他说是用刷机精灵线刷的rom包,但是刷机失败了,就来找我把弄好,他是个半吊子水平,刚接触这个东西,也是运气不好,什么刷机失败的问题都让他遇上了,刷了几个 ...

  5. php_html转译符号

    1.双引号 /" 或者 " 2.单引号 ' > 4. & &

  6. Spring4.1新特性——Spring缓存框架增强(转)

    目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...

  7. 个人纪录(初)----Ueditor文本编辑器的引用

    源码下载地址:http://ueditor.baidu.com/website/ 打开源码参考.html 把ueditor引入自己的web项目下. 在需要引入文本的地方写个div,设置样式等. 并且在 ...

  8. 转一个 Xcode 7 缺少 *.dylib库的解决方法

    Xcode7中 Link Binary With Libraries 没有 .dylib库,只能找到对应的 .tbd,但不能代替使用,通过查找资料,尝试后得到以下两种解决方法. 方法1. (heqin ...

  9. js2

    1.JS的三种输出方式: alert("");console.log("");document.write(""); 2.流程控制语句: a ...

  10. java多线程系列之 synchronized

    一.synchronized基本原理 java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁. ...