美文网首页
线程2 NSOperation 抽像类的使用

线程2 NSOperation 抽像类的使用

作者: 宁梓茞 | 来源:发表于2018-01-26 13:36 被阅读0次
    //
    //  ZYOperationViewController.h
    //  Thread
    //
    //  Created by ninglihuan on 14
    
    //
    //  ZYOperation.h
    //  Thread
    //
    //  Created by yejiong on 14/11/4.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface ZYOperation : NSOperation
    
    @property (nonatomic, copy) NSString* operationName;
    
    @end
    
    
    
    //
    //  ZYOperation.m
    //  Thread
    //
    //  Created by yejiong on 14/11/4.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ZYOperation.h"
    
    @implementation ZYOperation
    
    - (void)dealloc {
        [_operationName release];
        
        [super dealloc];
    }
    
    //重写 main 方法,在这里写入我们需要在线程中执行的代码。
    - (void)main {
    //    NSLog(@"--------%@", [NSThread currentThread]);
    //    
    //    if ([NSThread isMainThread]) {
    //        NSLog(@"主线程");
    //    }else {
    //        NSLog(@"分线程");
    //    }
        
        NSLog(@"%@", _operationName);
    }
    
    @end
    
     
    /11/4.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ZYOperationViewController : UIViewController
    
    @end
    
    //
    //  ZYOperationViewController.m
    //  Thread
    //
    //  Created by ninglihuan on 14/11/4.
    //  Copyright © 2014年 ninglihuan. All rights reserved.
    //
    
    #import "ZYOperationViewController.h"
    #import "ZYOperation.h"
    
    @interface ZYOperationViewController () {
        NSOperationQueue* _operationQueue;
        
    }
    
    @property (nonatomic, retain) NSBlockOperation* blockOperation1;
    
    @end
    
    @implementation ZYOperationViewController
    
    - (void)dealloc {
        [_operationQueue release];
        
        [_blockOperation1 release];
        
        [super dealloc];
    }
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
    
        _operationQueue = [[NSOperationQueue alloc] init];
        
        //设置最大并发数,同时最多有多少线程在执行。
        _operationQueue.maxConcurrentOperationCount = 3;
        
        
        //使用 NSBlockOperation 的时候需要注意是否造成循环引用。
        __block ZYOperationViewController* blockSelf = self;
        
        self.blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
            blockSelf.view.backgroundColor = [UIColor redColor];
        }];
        
        
        //没有造成循环引用。
        NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
            self.view.backgroundColor = [UIColor redColor];
        }];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //NSOperation 是一个抽象类,我们不能使用它的对象,必须使用它子类的对象。
        //系统提供了两个子类:NSInvocationOperation,NSBlockOperation。
        //我们也可以自定义 NSOperation 子类,重写 mian 方法,然后把 operation 对象添加到 NSOperationQueue 中,就会自动在分线程执行 main 方法。
        
        
    //    [self invocationOperation];
        
    //    [self blockOperation];
        
    //自定义
        [self customOperation];
    }
    
    - (void)invocationOperation {
        NSInvocationOperation* invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"123"];
        
        //使用 start 在主线程执行任务。
    //    [invocationOperation start];
        
        //如果需要在分线程执行任务,把对象添加到 NSOperationQueue,就会自动的在分线程执行任务。
        
        [_operationQueue addOperation:invocationOperation];
        
        [invocationOperation release];
    }
    
    - (void)blockOperation {
        NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"1--------%@", [NSThread currentThread]);
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        }];
        
        //主线程
    //    [blockOperation start];
        
        //分线程
    //    [_operationQueue addOperation:blockOperation];
        
        
        [blockOperation addExecutionBlock:^{
            NSLog(@"2--------%@", [NSThread currentThread]);
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        }];
        
        [blockOperation addExecutionBlock:^{
            NSLog(@"3--------%@", [NSThread currentThread]);
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        }];
    
        
        [blockOperation addExecutionBlock:^{
            NSLog(@"4--------%@", [NSThread currentThread]);
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        }];
    
        
        [blockOperation addExecutionBlock:^{
            NSLog(@"5--------%@", [NSThread currentThread]);
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        }];
    
        //addExecutionBlock,并发的执行多个 block 任务,系统自动分配到当前空闲的线程,可能是主线程也可能是分线程。
    //    [blockOperation start];
        
        //全部在分线程并发执行。
        [_operationQueue addOperation:blockOperation];
    }
    
    - (void)customOperation {
        ZYOperation* operation = [[ZYOperation alloc] init];
        
        operation.operationName = @"1111";
      
        
        ZYOperation* operation1 = [[ZYOperation alloc] init];
        
        operation1.operationName = @"2222";
        
        ZYOperation* operation2 = [[ZYOperation alloc] init];
        
        operation2.operationName = @"3333";
        
        
        //设置线程间的依赖关系。让一个线程等待另外一个线程执行完毕再开始执行。
        [operation addDependency:operation1];
        
        [_operationQueue addOperation:operation];
        [_operationQueue addOperation:operation1];
        [_operationQueue addOperation:operation2];
        
        [operation release];
        [operation1 release];
        [operation2 release];
    
    }
    
    - (void)run:(id)object {
        NSLog(@"%@", object);
    
        if ([NSThread isMainThread]) {
            NSLog(@"主线程");
        }else {
            NSLog(@"分线程");
        }
        
        //仍然使用这个方法回到主线程。
    //    self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:线程2 NSOperation 抽像类的使用

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