IOS NSOperation&NSOperationQueue
//
// ViewController.m
// CX-NSOperationQueue
//
// Created by ma c on 16/3/15.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// /*
GCD -- (iOS4.0)多线程解决方案
将任务(block)添加到队列(串行,并行(全局队列)),指定执行任务的方法。(同步,异步)
拿到dispatch_get_main_queue,线程之间的通信 NSOperation -- (iOS2.0) (后经苹果改造)
将操作添加到队列里
[NSOperationQueue mainQueue] 获取主队列,将任务添加到主队列,就会在主线程中执行
NSOperation可以设置最大并发数(用GCD处理,比较麻烦)
可以暂停可以继续,也就是挂起操作
取消所有的任务
设置依赖关系
*/ #import "ViewController.h" @interface ViewController ()
//NSOperation 队列
@property (nonatomic, strong) NSOperationQueue * queue; @end @implementation ViewController -(NSOperationQueue *)queue{ if (!_queue) {
_queue = [[NSOperationQueue alloc]init];
}
return _queue;
} - (void)viewDidLoad {
[super viewDidLoad];
//线程间通信
// [self contact];
//最大并发数 注意观察测试时间
[self maxCount]; //常用操作
/*
取消操作:- (void)cancelAllOperations;
挂起操作:@property (getter=isSuspended) BOOL suspended;
依赖关系:- (void)addDependency:(NSOperation *)op;
取消依赖:- (void)removeDependency:(NSOperation *)op;
*/ } -(void)maxCount{ self.queue.maxConcurrentOperationCount = ; for (NSInteger i = ; i < ; i ++) { NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil]; [self.queue addOperation:operation];
}
}
-(void)test{
[NSThread sleepForTimeInterval:];
NSLog(@"%@",[NSThread currentThread]);
}
//线程间通信
-(void) contact{ [self.queue addOperationWithBlock:^{ NSLog(@"%@",[NSThread currentThread]);
NSLog(@"One"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"Two");
}];
}];
}
- (void)test3 { //NSOperation是基于GCD的,把GCD的block封装成opertion,NSOperationQueue是全局队列封装
//将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步执行的。 //创建一个操作队列
NSOperationQueue * queue = [[NSOperationQueue alloc] init]; NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil]; NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"op2 - %@",[NSThread currentThread]);
}]; //添加NSOperation到NSOperationQueue中
//NSOperation添加到queue之后,通常短时间内就会得到运行。
// [queue addOperation:op];
// [queue addOperation:op2]; // waitUntilFinished yes 操作完成后执行下面的代码 no 先执行下面的代码 //添加一个block形式的operation
[queue addOperationWithBlock:^{ NSLog(@"op3 - %@",[NSThread currentThread]); }]; [queue addOperations:@[op,op2] waitUntilFinished:NO]; // NSLog(@"完成"); } - (void)test2 { // 能够并发地执行一个或多个block对象,所有相关的block都执行完之后,操作才算完成
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]); NSLog(@"第一个操作");
}]; // 通过addExecutionBlock方法添加block操作,开启多个线程
[op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第二个操作");
}]; [op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第三个操作");
}]; [op start];
} - (void)test1 { // 基于一个对象和selector来创建操作。如果你已经有现有的方法来执行需要的任务,就可以使用这个类 NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil]; // 如果我们想在一个NSOperation执行完毕后做一些事情,就调用NSOperation的setCompletionBlock方法来设置想做的事情
[op setCompletionBlock:^{
NSLog(@"完成");
}]; // 开始执行任务(同步执行)
// 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的。
[op start]; }
@end
*有关进程与线程我会进行总结*
*由于昨天网络出现问题,昨晚把代码写出来了,但是没有上传
IOS NSOperation&NSOperationQueue的更多相关文章
- iOS中的多线程NSThread/GCD/NSOperation & NSOperationQueue
iOS多线程有四套多线程方案: Pthreads NSThread GCD NSOperation & NSOperationQueue 接下来我来一个一个介绍他们 Pthreads 在类Un ...
- IOS多线程(NSOperation,NSOperationQueue)
含义:NSOperation,NSOperationQueue是什么. The NSOperation class is an abstract class you use to encapsulat ...
- iOS NSOperation
iOS NSOperation 一.简介 除了,NSThread和GCD实现多线程,配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOp ...
- iOS开发之NSOperation & NSOperationQueue
1.简介 (1) NSOperationQueue(操作队列)是由GCD提供的队列模型的Cocoa抽象,是一套Objective-C的API,为了使并发(多线程)编程变得更加简单,但效率比GCD略低. ...
- iOS NSOperation的使用
先给出NSOpetation的官方指导https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation ...
- iOS -NSOperation并发编程
http://www.cocoachina.com/game/20151201/14517.html http://blog.csdn.net/qinlicang/article/details/42 ...
- iOS NSOperation 封装 通知实现界面更新
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MYOperation : NSOpe ...
- iOS NSOperation 异步加载图片 封装NSOperation 代理更新
#import <Foundation/Foundation.h> @class MYOperation; @protocol MYOperationDelecate <NSObje ...
- iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别
In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...
随机推荐
- 我所研究过的 ASP.NET MVC 或者 .NET 或者 ORM 或者框架的开源项目
ASP.NET MVC 的开源项目有很多,这里列出我所研究过的: SocialGoal v1.0.0 prodinner nopCommerce SmartStore.NET 由于今天才做收集工作,可 ...
- 一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
介绍 不知道大家在使用 ASP.NET MVC 时有没有一些扩展要求,反正我是有很多.在使用 MVC 这几年(PS:我是从 1.0 开始学,2.0.3.0 开发至今),我深深地觉得 MVC 的扩展性真 ...
- When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
The most important difference is that the forIndexPath: version asserts (crashes) if you didn't regi ...
- HTML5 is Canvas
var cvs = document.getElementById("cvs"); // // 获取上下文环境 var con = cvs.getContext("2d& ...
- [SLAM] 01 "Simultaneous Localization and Mapping" basic knowledge
发信人: leecty (Terry), 信区: ParttimeJobPost标 题: 创业公司招SLAM 算法工程师发信站: 水木社区 (Thu Jun 16 19:18:24 2016), 站内 ...
- [Node.js] OAuth 2 和 passport框架
原文地址:http://www.moye.me/?p=592 OAuth是什么 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列 ...
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述) 用经典矩阵转置算法和普通的三元组矩阵转置在时间复杂度上都是不乐观的.快速转置算法在增加适当存储空间后实现快速转置具体原理见代码注释部 ...
- java基础---->java中正则表达式二
跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...
- struts2基础——自定义拦截器
一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...
- DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程
Java连接数据库的方法大体分为两种:正向连接和反向连接.反向连接需要编译器提供相关的插件来支持,目前主流的java IDE都支持反向连接.这里主要对正向连接做一个经验总结. 一.数据库的配置 1.新 ...