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 ...
随机推荐
- android 中targetSdkVersion和与target属性的区别
AndroidMenifest.xml中targetSdkVersion和project.properties中的target属性的区别 在AndroidMenifest.xml中,常常会有 ...
- 关于MySQL的Admin Ping Command
前言: 最近在线上诊断QPS飙升的过程中深入进行了下Admin Ping Command的测试.此外,再一些国外文章中最近也读到了一些相关知识,所以写成一篇博文做一下总结. 1. 关于Admin Pi ...
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...
- 想从事分布式系统,计算,hadoop等方面,需要哪些基础,推荐哪些书籍?--转自知乎
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
- 开放产品开发(OPD):OPD框架
在 开放产品开发(OPD):开篇 中讲了一下OPD是什么,以及它主要指引的方法,这篇文字将给大家介绍一下OPD框架. 一个公司有三种经营模式,像游戏代理的属于运营型,做企业定制项目管理软件的属于项目型 ...
- 妹味6:ajax与ajax封装
(功能)ajax能且仅能 从服务器读取文件 (环境)需要服务器环境才能测试,可以用工具建立本地服务器环境 (缓存)解决缓存问题:url加时间戳让每次请求地址唯一,如 url='abc.txt?t='+ ...
- Android学习笔记之使用百度地图实现Poi搜索
PS:装个系统装了一天.心力憔悴.感觉不会再爱了. 学习内容: 1.使用百度Map实现Poi搜索. 2.短串分享 3.在线建议查询 百度地图的研究也算是过半了.能够实现定位,实现相关信息的搜索,实 ...
- Android学习笔记之SoftReference软引用...
PS:其实这一篇和上一篇很类似,都是为了解决内存不足(OOM)这种情况的发生... 学习内容: 1.对象的引用类.... 最近也是通过项目中知道了一些东西,涉及到了对象的引用类,对象的引用类分为多 ...
- Week4 结对编程
1.照片 1.1 结对编程参与者:李文涛.黎柏文 1.2 展示照片 2.结对编程的优点&缺点 2.1 优点 2.1.1.两人分工合作,减少了工作量 2.1.2.结对编程的伙伴往往能提供不同 ...
- [Test] 单元测试艺术(1) 基础知识
单元测试不是软件开发的新概念,在1970年就一直存在,屡屡被证明是最理想的方法之一. 本系列将分成3节: 单元测试基础知识 打破依赖,使用模拟对象,桩对象,测试框架 创建优秀的单元测试 本节索引: 单 ...