iOS 开发多线程 —— NSOperation_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > iOS 开发多线程 —— NSOperation

iOS 开发多线程 —— NSOperation

 2017/8/15 15:31:21  Coding再Coding  程序员俱乐部  我要评论(0)
  • 摘要:本文是根据文顶顶老师的博客学习而来,转载地址:http://www.cnblogs.com/wendingding/p/3809042.html一、NSOperation简介1.简单说明NSOperation的作?:配合使用NSOperation和NSOperationQueue也能实现多线程编程NSOperation和NSOperationQueue实现多线程的具体步骤:(1)先将需要执行的操作封装到一个NSOperation对象中(2
  • 标签:operation iOS 多线程 开发 Opera 线程

本文是根据文顶顶老师的博客学习而来,转载地址:http://www.cnblogs.com/wendingding/p/3809042.html

一、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,实现内部相应的?法

 

NSInvocationOperation:

示例代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];
    [operation start];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
    [operation2 start];
    
}

-(void)thread1{
    NSLog(@"currentThread = %@",[NSThread currentThread]);
}

-(void)thread2{
   NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}
 currentThread = <NSThread: 0x17406d780>{number = 1, name = main}

currentThread2 = <NSThread: 0x17406d780>{number = 1, name = main}

可以看见,并没有开启新的线程.

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

 

NSBlockOperation:

 NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"operation = %@",[NSThread currentThread]);
    }];
    [operation3 start];



看打印结果:
operation = <NSThread: 0x1700728c0>{number = 1, name = main}

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

例如:

NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"operation = %@",[NSThread currentThread]);
    }];
    [operation3 addExecutionBlock:^{
        NSLog(@"operation2 = %@",[NSThread currentThread]);
    }];
    [operation3 start];


然后看打印结果:

operation = <NSThread: 0x1700706c0>{number = 1, name = main}
operation2 = <NSThread: 0x170075a40>{number = 4, name = (null)}

 

NSOperationQueue:

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

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

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

 

示例:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
   
    
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"currentThread3 = %@",[NSThread currentThread]);
    }];
    
    [operation3 addExecutionBlock:^{
        NSLog(@"currentThread3_1 = %@",[NSThread currentThread]);
    }];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //把操作添加到队列中
    //第一种方式
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    //第二种方式
    [queue addOperationWithBlock:^{
        NSLog(@"currentThread4 = %@",[NSThread currentThread]);
    }];
    
}

-(void)thread1{
    NSLog(@"currentThread1 = %@",[NSThread currentThread]);
}

-(void)thread2{
   NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}

查看结果:

currentThread1 = <NSThread: 0x17006f440>{number = 4, name = (null)}
currentThread2 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}
currentThread3_1 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}
currentThread3 = <NSThread: 0x17006f440>{number = 4, name = (null)}
currentThread4 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}

 

发表评论
用户名: 匿名