您的位置:首页 > 理论基础 > 计算机网络

网络多线程-NSOperation的简单使用

2015-11-27 21:43 399 查看
import "ViewController.h"

#import "XMGOperation.h"

@interface
ViewController ()

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self
customOperation];

}

-(void)customOperation

{

    NSLog(@"-----");

    //1.封装操作

    XMGOperation *op1 = [[XMGOperation
alloc]init];

    XMGOperation *op2 = [[XMGOperation
alloc]init];

    

    //2.启动执行

    [op1 start];

    [op2 start];

}

-(void)invocationOperation

{

    //1.封装操作

    /*

     第一个参数:目标对象SELF

     第二个参数:要调用的是哪个方法

     第三个参数:方法的参数

     */

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

    //2.启动执行操作

    [op1 start];

    

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

    //2.启动执行操作

    [op2 start];

    

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

    //2.启动执行操作

    [op3 start];

}

-(void)blockOperation

{

    //1.封装操作

    NSBlockOperation *op1 = [NSBlockOperation
blockOperationWithBlock:^{

        //主线程

        NSLog(@"1---%@",[NSThread
currentThread]);

      

    }];

    NSBlockOperation *op2 = [NSBlockOperation
blockOperationWithBlock:^{

        NSLog(@"2---%@",[NSThread
currentThread]);

    }];

    

    NSBlockOperation *op3 = [NSBlockOperation
blockOperationWithBlock:^{

        NSLog(@"3---%@",[NSThread
currentThread]);

    }];

    

    //添加额外任务

    [op3 addExecutionBlock:^{

        //子线程

        NSLog(@"4---%@",[NSThread
currentThread]);

    }];

    

    [op3 addExecutionBlock:^{

        NSLog(@"5---%@",[NSThread
currentThread]);

    }];

    

    //2.启动执行操作

    [op1 start];

    [op2 start];

    [op3 start];

}

-(void)run

{

    NSLog(@"run---%@",[NSThread
currentThread]);

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: