NSOperationQueue

 

#import <UIKit/UIKit.h>

@interface VCRoot : UIViewController

@end
#import "VCRoot.h"

@interface VCRoot ()
@property (assign,atomic) int count ;
@end

@implementation VCRoot
{
    //任務隊列
    //能夠將任務(線程的抽象)添加到隊列中來管理
    //能夠同時併發多個任務
    NSOperationQueue* _optQueue ;
    NSData* data  ;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //建立一個任務隊列
    //隊列中的元素爲任務對象
    //任務對象繼承於NSOperation
    _optQueue = [[NSOperationQueue alloc] init] ;
    
    //設置任務的最大併發數量
    [_optQueue setMaxConcurrentOperationCount:5] ;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //建立一個任務對象
    //將事件函數封裝到任務中
    //未來在隊列中執行
    NSInvocationOperation* tast01 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateOpt01) object:nil] ;
    //將任務添加到隊列中
    [_optQueue addOperation:tast01] ;
    
    
    //方法二
    //建立一個任務屬性對象
    NSInvocation* invo = [[NSInvocation alloc] init] ;
    //目標對象
    invo.target = self ;
    //事件函數
    invo.selector = @selector(updateOpt02) ;
    
    NSInvocationOperation* tast02 = [[NSInvocationOperation alloc] initWithInvocation:invo] ;
    //將第二個任務添加到隊列中
    [_optQueue addOperation:tast02] ;
    
    
    //方法三
    NSMutableArray* arrayTask = [[NSMutableArray alloc] init] ;
    for (int i = 0 ; i < 3; i++)
    {
        //建立多個任務,將任務添加到數組中
        NSInvocationOperation* task = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateOpt) object:nil] ;
    
        [arrayTask addObject:task] ;
    }
    //將任務數組添加到任務隊列中
    //是否等待以前的任務結束後開始執行
    [_optQueue addOperations:arrayTask waitUntilFinished:YES] ;
    
//    第四種方法,使用block來添加任務
//    添加一個block塊對象做爲任務隊列的任務
    [_optQueue addOperationWithBlock:^
    {
        int count = 0 ;
        for (int i = 0 ; i < 10000; i++)
        {
            count++ ;
            NSLog(@"count = %d",count) ;
        }
    }] ;
}

-(void) readFile
{
    data = [NSData dataWithContentsOfFile:@"/users/qianfeng/Desktop/office.zip"] ;
}

-(void) updateOpt01
{
    NSLog(@"任務一執行....");
    
    [data writeToFile:@"/users/qianfeng/Desktop/office01.zip" atomically:YES] ;
    
//    [NSThread exit] ;
    
    NSThread* t = [NSThread currentThread] ;
    [t cancel] ;
    while (true)
    {
        //休眠時間
        [NSThread sleepForTimeInterval:0.01] ;
        NSLock* lock = [[NSLock alloc] init] ;
        
        int k = 0 ;
        
        [lock lock] ;
        k++ ;
        [lock unlock] ;
        
        [NSThread isMainThread] ;
    }

}

-(void) updateOpt11
{
    NSLog(@"任務一執行....");
    
    [data writeToFile:@"/users/qianfeng/Desktop/office02.zip" atomically:YES] ;
}

-(void) updateOpt12
{
    NSLog(@"任務一執行....");
    
    [data writeToFile:@"/users/qianfeng/Desktop/office03.zip" atomically:YES] ;
}

-(void) updateOpt02
{
    NSLog(@"任務二執行...");
}

-(void) updateOpt
{
    NSLog(@"任務數組中的任務執行!");
    [NSThread sleepForTimeInterval:5];
}

@end
相關文章
相關標籤/搜索