• 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. 在VC++6.0开发中实现全屏显示

    全屏显示是一些应用软件程序必不可少的功能.比如在用VC++编辑工程源文件或编辑对话框等资源时,选择菜单“View\Full Screen”,即可进入全屏显示状态,按“Esc”键后会退出全屏显示状态. ...

  2. 《精通C#》十四章-.NET程序集入门

    在书中,这一章节的开头说的是自定义命名空间和使用命名空间,在以我目前有限的经验来说,程序集就是一个类库经过编译之后,所生成的一个在引用命名空间,进而使用该文件中已经定义好的字段,属性以及方法的文件,以 ...

  3. c#连接vertica数据库

    项目前期使用mysql数据库,大约每天200w数据量,十天1500w数据量之后,读取写入都会很慢,而且经常锁表,后来采用vertica数据库,下面分享vertica数据库使用方法,以及大批量数据快速写 ...

  4. 与Java互操作

    课程内容涵盖了Java互操作性. Javap 类 异常 特质 单例对象 闭包和函数 变化性 Javap javap的是JDK附带的一个工具.不是JRE,这里是有区别的. javap反编译类定义,给你展 ...

  5. Anjs分词器以及关键词抓取使用的方法

    首先介绍一下这个网址非常有用本文所有的关于Anjs起源来自这里请先查看一下 https://github.com/NLPchina/ansj_seg 在本次测试使用的是     import java ...

  6. 在 Windows 上安装 Laravel 5.x

    由于工作中要用到 Laravel ,所以在此记录以备忘. 下载并设置 PHP 进入 http://windows.php.net/download/ 下载 PHP 5.5.x 的最新版本.如果你用的是 ...

  7. Jupyter Notebook 27绝技——27 Jupyter Notebook tips, tricks and shortcuts

    转载自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ Jupyter notebook, formerly ...

  8. [转]查看Flash Log输出

    1.安装Debug版本的FlashPlayer 2.mm.cfg配置文件 xp:C:\Documents and Settings\username\mm.cfg Vista/7: C:\users\ ...

  9. 鼠标滚动div固定浮动-加锚点

    页面:    <div class="pa">        <div class="w-95-sl bdl-2"><a>标 ...

  10. eap-peap/mschapv2

    eap-peap/mschapv2       文件路径 用途 示例 备注 #gedit /usr/local/etc/raddb/sites-available/default #gedit /us ...