1.  NSOperation实现多线程编程,需要和NSOperationQueue一起使用。

(1)先将要执行的操作封装到NSOperation中

(2)将NSOperation对象添加到NSOperationQueue中

(3)系统将自动将NSOPeration从NSOperationQueue中取出来,放到一个新的线程中去执行

2.NSOperation 的子类

  NSOperation是一个抽象类,并没有封装操作的能力。因此要用到它的子类:

(1)NSInvocationOperation

  (2)NSBlockOperation

3. NSInvocationOperation 和NSBlockOperation  的创建

(1)NSInvocationOperation的创建

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];

    [operation start];

2017-06-18 10:17:04.165 demo[17389:3000329] 现在的线程----<NSThread: 0x60000007bf80>{number = 1, name = main}

 注:操作对象默认在主线程中执行,只有加入到队列中才会开启新的线程。既默认情况下,如果操作没有放到Queue中只是同步执行,只有放到了NSOperationQueue中才会异步执行。

(2)NSBlockOperation的创建

(1)代码1

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
}]; [operation start];

打印结果:

2017-06-18 10:20:19.440 demo[17409:3003510] 现在的线程----<NSThread: 0x6080000713c0>{number = 1, name = main}

(2)代码2

- (void)viewDidLoad {
[super viewDidLoad]; NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
}]; [operation addExecutionBlock:^{
NSLog(@"addExecution 的线程 ----%@",[NSThread currentThread]); }]; [operation start];
}

2017-06-18 10:26:31.545 demo[17436:3012476] 现在的线程----<NSThread: 0x608000079900>{number = 1, name = main}

2017-06-18 10:26:31.545 demo[17436:3012571] addExecution 的线程  ----<NSThread: 0x600000262f00>{number = 3, name = (null)}

 注:NSBlockOperation 封装的操作数>1  那么就会开启新的线程。

 

4.NSOperationQueue

NSOperationQueue是操作对象的队列。将NSOperation对象放到NSOperationQueue里,系统会自动取出NSOperation,开启新线程 。

(1)NSOperationQueue添加操作的方式

    //定义队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//将操作添加到队列
[queue addOperation:operation];
[queue addOperation:operation1];
[queue addOperation:operation2];
//用block的方式添加操作
[queue addOperationWithBlock:^{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
}];

(2)NSOperationQueue开启线程的方式

//
// ViewController.m
// demo
//
// Created by 登 on 2017/6/17.
// Copyright © 2017年 登. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil]; NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction1) object:nil]; NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
}]; [operation2 addExecutionBlock:^{
NSLog(@"addExecution 的线程 ----%@",[NSThread currentThread]); }]; //定义队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//将操作添加到队列
[queue addOperation:operation];
[queue addOperation:operation1];
[queue addOperation:operation2];
//用block的方式添加操作
[queue addOperationWithBlock:^{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
}]; } -(void)testAction
{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
} -(void)testAction1
{
NSLog(@"现在的线程----%@",[NSThread currentThread]);
} @end

2017-06-18 10:49:44.163 demo[17479:3030821] 现在的线程----<NSThread: 0x60000007c380>{number = 5, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030826] 现在的线程----<NSThread: 0x60000007c040>{number = 3, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030820] 现在的线程----<NSThread: 0x60000007c2c0>{number = 4, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030842] 现在的线程----<NSThread: 0x60000007c540>{number = 6, name = (null)}

2017-06-18 10:49:44.164 demo[17479:3030846] addExecution 的线程  ----<NSThread: 0x60800006ec40>{number = 7, name = (null)}

注:

1.NSOPerationQueue将操作取出,每一个操作都会开启一个新的线程。所有的操作都是异步并发执行。

2.其中Operation2用NSBlockOPeration添加了两个任务,这两个任务开启两个线程,同样并发执行。

iOS多线程---NSOperation介绍和使用的更多相关文章

  1. iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信

    一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加 ...

  2. iOS 多线程 NSOperation、NSOperationQueue

    1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...

  3. iOS多线程 NSOperation的用法

    上一篇写了 GCD 的使用,接下来就了解一下 NSOperation ,NSOperation是苹果对 GCD 的 OC 版的一个封装,但是相对于GCD来说可控性更强,并且可以加入操作依赖. NSOp ...

  4. iOS多线程--NSOperation 浅显易懂

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  5. iOS多线程--NSOperation

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  6. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...

  7. iOS多线程---NSOperation的常用操作

    1.最大并发数: - (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger) ...

  8. iOS多线程编程

    废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...

  9. [转] iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用

    介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首 ...

随机推荐

  1. client.HConnectionManager$HConnectionImplementation: Can't get connection to ZooKeeper: KeeperErrorCode = ConnectionLoss for /hbase

    解决方法:hbase 未成功启动 1.关闭防火墙:service iptables stop 2.start-hbase.sh

  2. highcharts点击事件系列

    http://www.highcharts.com/demo/line-ajax 参考设置(bar 柱状图) plotOptions: {                series: {       ...

  3. [GO]解决golang.org/x/ 下包下载不下来的问题

    因为在项目中要使用到一个golang.org的包,但是因为墙的问题,官方方法已经无法使用,但是在github上存在一个镜像站可以使用,我们只需要将它克隆下来就可以正常使用了 mkdir -p $GOP ...

  4. 安装memcache服务

    d:\tools\memcache\setup\memcached -d install

  5. golang的json操作[转]

    package main   import (     "encoding/json"     "fmt"     "os" )   typ ...

  6. 201709021工作日记--CAS解读

    CAS主要参考博文:classtag  http://www.jianshu.com/p/473e14d5ab2d CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术 ...

  7. Hibernate 的复杂用法HibernateCallback

    HibernateTemplate还提供了一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.HibernateTemplate的灵活访问方式可通过如下两个方法完成: ...

  8. delphi API: SetWindowPos改变窗口的位置与状态

    SetWindowPos 函数功能:该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序.子窗口,弹出式窗口,及顶层窗口根据它们在屏幕上出现的顺序排序.顶层窗口设置的级别最高,并且被设置为Z序 ...

  9. CentOS7 Docker 安装

    CentOS7 已经内置了docker ,可以直接安装 安装Docker 命令: sudo yum install -y docker  启动docker  命令: service docker st ...

  10. List<T>用法

    所属命名空间:System.Collections.Generic public class List<T> : IList<T>, ICollection<T>, ...