zoukankan      html  css  js  c++  java
  • iOS开发多线程篇 09 —NSOperation简单介绍

    iOS开发多线程篇—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,实现内部相应的⽅法

    二、 具体说明

    1.NSInvocationOperation子类

    创建对象和执行操作:

    复制代码
    1  //创建操作对象,封装要执行的任务
    2     //NSInvocationOperation   封装操作
    3     NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
    4     
    5     //执行操作
    6     [operation start];
    复制代码

    说明:一旦执⾏操作,就会调用target的test方法

    代码示例:

    复制代码
     1 //
     2 //  YYViewController.m
     3 //  01-NSOperation基本1
     4 //
     5 //  Created by 孔医己 on 14-6-25.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYViewController.h"
    10 
    11 @interface YYViewController ()
    12 
    13 @end
    14 
    15 @implementation YYViewController
    16 
    17 - (void)viewDidLoad
    18 {
    19     [super viewDidLoad];
    20     
    21     //NSOperation:抽象类,不具备封装功能
    22     
    23     //创建操作对象,封装要执行的任务
    24     //NSInvocationOperation   封装操作
    25     NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
    26     
    27     //执行操作
    28     [operation start];
    29 
    30 }
    31 
    32 -(void)test
    33 {
    34     
    35     NSLog(@"--test--%@--",[NSThread currentThread]);
    36 }
    37 @end
    复制代码

    打印查看:

    注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作 

     

    2.NSBlockOperation子类

    创建对象和添加操作:

    复制代码
     1   //创建NSBlockOperation操作对象
     2     NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
     3         //......
     4     }];
     5     
     6     //添加操作
     7     [operation addExecutionBlock:^{
     8         //....
     9     }];
    10     
    复制代码

    代码示例:

    代码1:

    复制代码
     1 //
     2 //  YYViewController.m
     3 //  02-NSTherad基本2
     4 //
     5 //  Created by 孔医己 on 14-6-25.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYViewController.h"
    10 
    11 @interface YYViewController ()
    12 
    13 @end
    14 
    15 @implementation YYViewController
    16 
    17 - (void)viewDidLoad
    18 {
    19     [super viewDidLoad];
    20     
    21     //创建NSBlockOperation操作对象
    22     NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
    23         NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
    24     }];
    25     
    26     
    27     //开启执行操作
    28     [operation start];
    29 }
    30 @end
    复制代码

    打印查看:

    代码2:

    复制代码
     1 //
     2 //  YYViewController.m
     3 //  02-NSTherad基本2
     4 //
     5 //  Created by 孔医己 on 14-6-25.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYViewController.h"
    10 
    11 @interface YYViewController ()
    12 
    13 @end
    14 
    15 @implementation YYViewController
    16 
    17 - (void)viewDidLoad
    18 {
    19     [super viewDidLoad];
    20     
    21     //创建NSBlockOperation操作对象
    22     NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
    23         NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
    24     }];
    25     
    26     //添加操作
    27     [operation addExecutionBlock:^{
    28         NSLog(@"NSBlockOperation1------%@",[NSThread currentThread]);
    29     }];
    30     
    31     [operation addExecutionBlock:^{
    32         NSLog(@"NSBlockOperation2------%@",[NSThread currentThread]);
    33     }];
    34     
    35     //开启执行操作
    36     [operation start];
    37 }
    38 @end
    复制代码

    注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作 

     

    3.NSOperationQueue

    NSOperationQueue的作⽤:NSOperation可以调⽤start⽅法来执⾏任务,但默认是同步执行的

    如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作

    添加操作到NSOperationQueue中,自动执行操作,自动开启线程

     

    复制代码
     1     //创建NSOperationQueue
     2     NSOperationQueue * queue=[[NSOperationQueue alloc]init];
     3     //把操作添加到队列中
     4     //第一种方式
     5     [queue addOperation:operation1];
     6     [queue addOperation:operation2];
     7     [queue addOperation:operation3];
     8     //第二种方式
     9     [queue addOperationWithBlock:^{
    10         NSLog(@"NSBlockOperation3--4----%@",[NSThread currentThread]);
    11     }];
    复制代码

     

    - (void)addOperation:(NSOperation *)op;
    - (void)addOperationWithBlock:(void (^)(void))block; 

    代码示例:

    复制代码
     1 //
     2 //  YYViewController.m
     3 //  03-NSOperation基本3
     4 //
     5 //  Created by 孔医己 on 14-6-25.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYViewController.h"
    10 
    11 @interface YYViewController ()
    12 
    13 @end
    14 
    15 @implementation YYViewController
    16 
    17 - (void)viewDidLoad
    18 {
    19     [super viewDidLoad];
    20 
    21     //创建NSInvocationOperation对象,封装操作
    22     NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    23     NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    24     //创建对象,封装操作
    25     NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
    26         NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
    27     }];
    28     [operation3 addExecutionBlock:^{
    29         NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
    30     }];
    31     
    32     //创建NSOperationQueue
    33     NSOperationQueue * queue=[[NSOperationQueue alloc]init];
    34     //把操作添加到队列中
    35     [queue addOperation:operation1];
    36     [queue addOperation:operation2];
    37     [queue addOperation:operation3];
    38 }
    39 
    40 -(void)test1
    41 {
    42     NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
    43 }
    44 
    45 -(void)test2
    46 {
    47     NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
    48 }
    49 
    50 @end
    复制代码

    打印效果:

    注意:系统自动将NSOperationqueue中的NSOperation对象取出,将其封装的操作放到一条新的线程中执行。上面的代码示例中,一共有四个任务,operation1和operation2分别有一个任务,operation3有两个任务。一共四个任务,开启了四条线程。通过任务执行的时间全部都是273可以看出,这些任务是并行执行的。

    提示:队列的取出是有顺序的,与打印结果并不矛盾。这就好比,选手A,BC虽然起跑的顺序是先A,后B,然后C,但是到达终点的顺序却不一定是A,B在前,C在后。

    下面使用for循环打印,可以更明显的看出任务是并发执行的。

    代码示例:

    复制代码
     1 #import "YYViewController.h"
     2 
     3 @interface YYViewController ()
     4 
     5 @end
     6 
     7 @implementation YYViewController
     8 
     9 - (void)viewDidLoad
    10 {
    11     [super viewDidLoad];
    12 
    13     //创建NSInvocationOperation对象,封装操作
    14     NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    15     NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    16     //创建对象,封装操作
    17     NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
    18         for (int i=0; i<5; i++) {
    19             NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
    20         }
    21     }];
    22     [operation3 addExecutionBlock:^{
    23         for (int i=0; i<5; i++) {
    24         NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
    25         }
    26     }];
    27     
    28     //创建NSOperationQueue
    29     NSOperationQueue * queue=[[NSOperationQueue alloc]init];
    30     //把操作添加到队列中
    31     [queue addOperation:operation1];
    32     [queue addOperation:operation2];
    33     [queue addOperation:operation3];
    34 }
    35 
    36 -(void)test1
    37 {
    38     for (int i=0; i<5; i++) {
    39     NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
    40     }
    41 }
    42 
    43 -(void)test2
    44 {
    45     for (int i=0; i<5; i++) {
    46     NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
    47     }
    48 }
    49 
    50 @end
    复制代码

     

     
     
  • 相关阅读:
    win7下安装memcached
    Integer自动装拆箱
    XShell配色方案
    XShell上传和下载
    基于注解的Spring AOP拦截含有泛型的DAO
    PAT Advance 1020
    PAT Advance 1014
    JavaWeb中文乱码问题解决思路
    eclipse启动错误
    操作系统(一)
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5628244.html
Copyright © 2011-2022 走看看