码迷,mamicode.com
首页 > 编程语言 > 详细

NSOperation多线程

时间:2015-11-22 21:55:47      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

 

  1 //
  2 //  ViewController.m
  3 //  NSOperation
  4 //
  5 //  Created by ys on 15/11/22.
  6 //  Copyright (c) 2015年 ys. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()
 12 @property(nonatomic,strong)NSOperationQueue *queue;//自定义队列
 13 @end
 14 
 15 @implementation ViewController
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19     self.queue = [[NSOperationQueue alloc]init];
 20     
 21     [self operationDemo1];
 22 //    [self operationDemo2];
 23 //    [self operationDemo3];
 24 //    [self operationDemo4];
 25 //    [self operationDemo5];
 26     
 27 }
 28 //     所有的自定义队列,都是在子线程中运行
 29 -(void)operationDemo1//NSBlockOperation
 30 {
 31 //    添加到自定义队列
 32     NSBlockOperation *blockOP = [NSBlockOperation blockOperationWithBlock:^{
 33         NSLog(@"%@", [NSThread currentThread]);
 34     }];
 35     [self.queue addOperation:blockOP];
 36     
 37     NSLog(@"%@", [NSThread currentThread]);
 38 }
 39 
 40 -(void)operationDemo2//NSBlockOperation
 41 {
 42     //添加到主(线程)队列
 43     [[NSOperationQueue mainQueue]addOperationWithBlock:^{
 44         NSLog(@"%@", [NSThread currentThread]);
 45     }];
 46     
 47     NSLog(@"%@", [NSThread currentThread]);
 48 }
 49 
 50 -(void)operationDemo3//NSBlockOperation
 51 {
 52     //添加多个线程到队列
 53     for (int i = 0; i < 10; ++i) {
 54         [self.queue addOperationWithBlock:^{//默认并行队列并发执行
 55             NSLog(@"%@--%d", [NSThread currentThread], i);
 56         }];
 57     }
 58     
 59     NSLog(@"%@", [NSThread currentThread]);
 60 }
 61 
 62 -(void)operationDemo4//NSBlockOperation
 63 {
 64     NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
 65         NSLog(@"下载图片 %@", [NSThread currentThread]);
 66     }];
 67     NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
 68         NSLog(@"修饰图片 %@", [NSThread currentThread]);
 69     }];
 70     NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
 71         NSLog(@"保存图片 %@", [NSThread currentThread]);
 72     }];
 73     NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
 74         NSLog(@"更新UI %@", [NSThread currentThread]);
 75     }];
 76     
 77     // 设定执行顺序, Dependency依赖,可能会开多个,但不会太多
 78     // 依赖关系是可以跨队列的
 79     [op2 addDependency:op1];
 80     [op3 addDependency:op2];
 81     [op4 addDependency:op3];
 82     // GCD是串行队列,异步任务,只会开一个线程
 83     
 84     [self.queue addOperation:op1];
 85     [self.queue addOperation:op2];
 86     [self.queue addOperation:op3];
 87     // 所有UI的更新需要在主线程上进行.此处实现夸队列,体现了NSOperation的强大便捷
 88     [[NSOperationQueue mainQueue] addOperation:op4];
 89 }
 90 
 91 
 92 -(void)operationDemo5//NSInvocationOP
 93 {
 94     // 需要定义一个方法,能够接收一个参数
 95     // 使用起来不够灵活
 96     NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(demo4selector:) object:@"operationDemo4"];
 97     [self.queue addOperation:op];
 98 }
 99 
100 -(void)demo4selector:(id)obj
101 {
102     NSLog(@"%@--%@", [NSThread currentThread],obj);
103 }
104 @end

 

NSOperation多线程

标签:

原文地址:http://www.cnblogs.com/yangshun-work/p/4986795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!