美文网首页
NSOperation-2

NSOperation-2

作者: Sunney | 来源:发表于2017-10-10 17:12 被阅读6次

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    //创建队列

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    //设置最大并发操作数(线程用完了还可以拿来用)

    queue.maxConcurrentOperationCount = 3;//数量为1的时候是串行队列,大于1的时候是并发队列

    //NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{

    //NSLog(@"download1====%@",[NSThread currentThread]);

    //}];

    //添加操作到队列中

    //[queue addOperation:op1];

    [queue addOperationWithBlock:^{

    NSLog(@"download1====%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:0.01];

    }];

    [queue addOperationWithBlock:^

    NSLog(@"download2====%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:0.01];

    }];

    [queue addOperationWithBlock:^{

    NSLog(@"download3====%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:0.01];

    }];

    [queue addOperationWithBlock:^{

    NSLog(@"download4====%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:0.01];

    }];

    [queue addOperationWithBlock:^{

    NSLog(@"download5====%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:0.01];

    }];

    }

    -(void)operationQueue1{

    //创建队列

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    //创建操作(任务) NSInvocationOperation

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

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

    //NSBlockOperation

    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{

    NSLog(@"download3====%@",[NSThread currentThread]);

    }];

    [op3 addExecutionBlock:^{

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

    }];

    [op3 addExecutionBlock:^{

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

    }];

    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{

    NSLog(@"download6===========%@",[NSThread currentThread]);

    }];

    //创建XMGOperation 创建一个类名叫XMGOperation继承于NSOperation

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

    //将任务添加懂啊队列中

    [queue addOperation:op1];//[op1 start];

    [queue addOperation:op2];//[op2 start];

    [queue addOperation:op3];//[op3 start];

    [queue addOperation:op4];//[op4 start];

    [queue addOperation:op5];//[op5 start];

    }

    -(void)download1{

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

    }

    -(void)download2{

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

    }

    //XMGOperation类

    #import"XMGOperation.h"

    @implementation XMGOperation

    /**

    需要执行的任务

    **/

    //当异步操作重复调用main里面的代码

    -(void)main{

    NSLog(@“XMGOperation ----下载图片-------%@”,[NSThread currentThread]);

    }

    相关文章

      网友评论

          本文标题:NSOperation-2

          本文链接:https://www.haomeiwen.com/subject/tqnwdttx.html