线程9--NSOperation
一、NSOperation简介
1.简单说明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
(1)先将需要执行的操作封装到一个NSOperation对象中
(2)然后将NSOperation对象添加到NSOperationQueue中
(3)系统会⾃动将NSOperationQueue中的NSOperation取出来
(4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法
/*************************************************************/
//即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作
NSLog(@"主线程---%@",[NSThread currentThread]);
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
[operation1 start];
/*************************************************************/
//注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation1----%@",[NSThread currentThread]);}
}]; [operation2 addExecutionBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation2----%@",[NSThread currentThread]);}
}];
[operation2 addExecutionBlock:^{
for (int i=; i<; i++) { NSLog(@"NSBlockOperation3----%@",[NSThread currentThread]); }
}];
//放入队列后就不需要这句;
[operation2 start]; /*************************************************************/
/*1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation对象添加到NSOperationQueue中 (3)系统会⾃动将NSOperationQueue中的NSOperation取出来 (4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
*/ NSInvocationOperation *operation3=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test3) object:nil];
NSInvocationOperation *operation4=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test4) object:nil]; NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[queue addOperation:operation2];
[queue addOperation:operation3];
[queue addOperation:operation4];
/*************************************************************/
-(void)test1{
NSLog(@"operation1--test1---%@",[NSThread currentThread]);
}
-(void)test3{
for (int i=; i<; i++) {
NSLog(@"NSOperationQueue-test3----%@",[NSThread currentThread]);
}
}
-(void)test4{
for (int i=; i<; i++) {
NSLog(@"NSOperationQueue--test4---%@",[NSThread currentThread]);
}
}
线程9--NSOperation的更多相关文章
- NSThread 子线程 Cocoa NSOperation GCD(Grand Central Dispatch) 多线程
单词:thread 英 θred:n 线.思路.vt 穿过.vi 穿透过 一. 进程.线程 进程:正在进行中的程序被称为进程,负责程序运行的内存分配,每一个进程都有自己独立的虚拟内存空间 线程: ...
- 创建线程方式-NSOperation
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 线程2 NSOperation 抽像类的使用
// // ZYOperationViewController.h // Thread // // Created by yejiong on 14 // // ZYOperation.h // Th ...
- IOS-多线程(NSOperation)
一.基础用法 // // ViewController.m // IOS_0120_NSOperation // // Created by ma c on 16/1/20. // Copyright ...
- 4.4 多线程进阶篇<下>(NSOperation)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人"简书" 本文源码 Demo 详见 Github https://github.c ...
- NSOperation、NSOperationQueue(II)
NSOperationQueue 控制串行执行.并发执行 NSOperationQueue 创建的自定义队列同时具有串行.并发功能 这里有个关键属性 maxConcurrentOperationCou ...
- iOS线程开发小结
在iOS开发线程操作时,一般方法名决定是否开启新线程(async,sync),队列类型(全局队列,串行队列)决定开启多少条线程 1.快速线程调用 *开启后台线程执行任务 [self performSe ...
- iOS线程之——NSCondition
多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美.这篇文章主要从线程创建与启动.线程的同步与锁.线程的交互.线 ...
- iOS:操作队列实现多线程NSOperation
NSOperation具体使用:直接继承NSObject 它的子类有:NSBlockOperation.NSInvocationOperation 还有一个必须的类,队列,用来装创建的线程 NSOpe ...
- iOS线程浅析
一.线程概述 1. iOS里面的线程按种类可分为同步线程和异步线程.同步线程指调用同步线程的地方必须等到同步线程运行完成才干够继续向下运行.而调用异步线程的地方则在运行完调用异步线程的语句后就能够继续 ...
随机推荐
- xt
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 错误:Parameter '0' not found.Available parameters are [arg1, arg0, param1, param2]的解决方法
调用的方法: List<Card> temp = cardService.queryRepeat(Type,shop); xml: <select id="queryRep ...
- Spring Boot项目搭建
1.Spring Boot概述 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- 使用session的监听器获取当前在线人数
1首先在web.xml中配置Session的监听器 2创建监听器并且继承HttpSessionListener 3.在jsp中导入监听器 4.获取当前在线人数 5.配置到公共网络(使用natapp的免 ...
- 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- 【文文殿下】【BZOJ4804】欧拉心算
题解 显然有 \(ans=\sum _{i=1} ^{n} \lfloor \frac{n}{i} \rfloor \sum _{d|i} \mu(d) \phi (\frac{i}{d})\) 前半 ...
- Java对象的大小及应用类型
基础类型数据的大小是固定的,对于非基本类型的java对象,其大小就值得商榷了. 在java中一个空Object对象的大小是8byte,这个大小只是保存堆中没有任何属性的对象的大小,看下面的语 ...
- linux安装git,linux安装jenkins
首先是两个地址,分别是git的版本下载地址,jenkins的下载地址 https://mirrors.edge.kernel.org/pub/software/scm/git/ http://mirr ...
- 从码农升为PM(节约成本)
做为一个码农的潜规则,用户怎么要求怎么写,不论过程只论是否符合要求以及减少bug的存在,虽然bug随时会出现,这就是码农,一直以来都说码农分很多种但个人认为就是一种,原因是码农不懂的换位思考,不懂的在 ...