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 ...
随机推荐
- Java知多少(108)数据库查询简介
利用Connection对象的createStatement方法建立Statement对象,利用Statement对象的executeQuery()方法执行SQL查询语句进行查询,返回结果集,再形如g ...
- C中调用Lua函数
我们先来看一个简单的例子: lua_State* L = NULL; // 内部调用lua函数 double f(double x, double y) { double z; lua_getglob ...
- Unity 全面理解加载和内存管理
最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质上我理解没有什么区别.Resources ...
- codeforces George and Job
/* 题意:给一个长度为n的序列, 从中选择长度为m的k个区间(任意两个区间不会有公共部分) 使得所选择的区间的和最大! 思路:这是一种很常见的dp dp[i][j] 表示的是前 i 个数选择 j 个 ...
- excel导入记录
use DangJianSELECT vale1, value2 into Table2 from Table1 select COUNT(*) from tmpdangyuan where 手机号 ...
- RTX发送消息提醒实现以及注意事项
一.RTX简介 RTX是腾讯公司推出的企业级即时通信平台.该平台定位于降低企业通信费用,增强企业内部沟通能力,改善企业与客户之间的沟通渠道,创造新兴的企业沟通文化,提高企业生产力.RTX平台的主要功能 ...
- selenium webdriver (python) 第一版PDF
前言 如果你是一位有python语言基础的同学,又想通过python+ selenium去实施自动化,那么你非常幸运的找到了这份文档,我也非常荣幸能为你的自动化学习之路带来一丝帮助. 其实,我在sel ...
- 值得拥有!精心推荐几款超实用的 CSS 开发工具
当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 CSS 开发工 ...
- MEF(Managed Extensibility Framework )的入门介绍
1.什么是MEF MEF是一个来自于微软协作构建扩展应用的新框架,它的目的是在运行中的应用中添加插件.MEF继承于.NET 4.0 Framework平台,存在于各种应用平台的系统程序集中 2.程序集 ...
- Linux - Ubuntu下JDK配置
系统版本: ubuntu 14.04 x64JDK版本: jdk-8u60-linux-x64 1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应的JDK文件,我这里下 ...