• NSOperationQueue 的整理


    @interface YkMainViewController : UIViewController <YkFlipsideViewControllerDelegate>
    {
        NSOperationQueue *operationQueue; 
        BOOL b_init;
    }
    
    @interface MyTask : NSOperation
    {
        int operationId;
    }
    
    @property int operationId;
    @end
    
    #import "MyTask.h"
    
    @implementation MyTask
    @synthesize operationId;
    
    - (void)main{ 
        NSLog(@"task %i is begin run … ",operationId); 
        [NSThread sleepForTimeInterval:10]; 
        NSLog(@"task %i is run finished. ",operationId); 
    }
    
    
    @end
    

    ---------开始使用:场景1:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        
        if(!b_init)
        {
            operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:1];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            [operationQueue addOperation:_task1];
        }        
        b_init = YES;
    }
    

     结果:

    2011-11-24 11:23:34.222 NSOperationQueueTest[2719:12d0b] task 1 is begin run …
    2011-11-24 11:23:44.225 NSOperationQueueTest[2719:12d0b] task 1 is run finished.

    场景2:

           operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:1];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            [operationQueue addOperation:_task1];
            
            MyTask * _task2 = [[[MyTask alloc] init] autorelease];
            _task2.operationId = index++; 
            
            [operationQueue addOperation:_task2];
    

     结果:

    2011-11-24 11:24:40.516 NSOperationQueueTest[2769:12e0b] task 1 is begin run …
    2011-11-24 11:24:50.518 NSOperationQueueTest[2769:12e0b] task 1 is run finished.
    2011-11-24 11:24:50.527 NSOperationQueueTest[2769:12e0b] task 2 is begin run …
    2011-11-24 11:25:00.529 NSOperationQueueTest[2769:12e0b] task 2 is run finished.

    场景3:

         operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:2];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            [operationQueue addOperation:_task1];
            
            MyTask * _task2 = [[[MyTask alloc] init] autorelease];
            _task2.operationId = index++;
            
            [operationQueue addOperation:_task2];
    

    结果:

    2011-11-24 11:25:41.663 NSOperationQueueTest[2815:11503] task 1 is begin run …
    2011-11-24 11:25:41.663 NSOperationQueueTest[2815:12d0b] task 2 is begin run …
    2011-11-24 11:25:51.667 NSOperationQueueTest[2815:11503] task 1 is run finished.
    2011-11-24 11:25:51.669 NSOperationQueueTest[2815:12d0b] task 2 is run finished.

    或者

    2011-11-24 11:25:41.663 NSOperationQueueTest[2815:11503] task 1 is begin run …
    2011-11-24 11:25:41.663 NSOperationQueueTest[2815:12d0b] task 2 is begin run …
    2011-11-24 11:25:51.669 NSOperationQueueTest[2815:12d0b] task 2 is run finished.
    2011-11-24 11:25:51.670 NSOperationQueueTest[2815:11503] task 1 is run finished.

    。。。

    场景4:

      operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:2];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            [operationQueue addOperation:_task1];
            
            MyTask * _task2 = [[[MyTask alloc] init] autorelease];
            _task2.operationId = index++;
            [_task2 addDependency:_task1]; 
            
            [operationQueue addOperation:_task2];
    

     结果:

    2011-11-24 11:27:00.820 NSOperationQueueTest[2859:11603] task 1 is begin run …
    2011-11-24 11:27:10.823 NSOperationQueueTest[2859:11603] task 1 is run finished.
    2011-11-24 11:27:10.830 NSOperationQueueTest[2859:13507] task 2 is begin run …
    2011-11-24 11:27:20.832 NSOperationQueueTest[2859:13507] task 2 is run finished.

    场景5:

          operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:2];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            
            
            MyTask * _task2 = [[[MyTask alloc] init] autorelease];
            _task2.operationId = index++;
            [_task2 addDependency:_task1]; 
            
            
            [operationQueue addOperation:_task2]; [operationQueue addOperation:_task1];
    

     结果:

    2011-11-24 11:29:19.807 NSOperationQueueTest[2945:11503] task 1 is begin run …
    2011-11-24 11:29:29.809 NSOperationQueueTest[2945:11503] task 1 is run finished.
    2011-11-24 11:29:29.817 NSOperationQueueTest[2945:12d0f] task 2 is begin run …
    2011-11-24 11:29:39.818 NSOperationQueueTest[2945:12d0f] task 2 is run finished.

     场景6:

     operationQueue = [[NSOperationQueue alloc] init];
            [operationQueue setMaxConcurrentOperationCount:2];  
            
            int index = 1;
            MyTask * _task1 = [[[MyTask alloc] init] autorelease];
            _task1.operationId = index++;
            
            
            MyTask * _task2 = [[[MyTask alloc] init] autorelease];
            _task2.operationId = index++;
            [_task2 addDependency:_task1]; 
            
            
            [operationQueue addOperation:_task2];
            [operationQueue addOperation:_task1];
            
            NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomethingInBackground) object:nil];
            
            [operationQueue addOperation:operation]; 
    - (void) doSomethingInBackground
    {
        NSLog(@"doSomethingInBackground");
    }
    

    结果:

    2011-11-24 11:32:15.211 NSOperationQueueTest[3038:11503] doSomethingInBackground
    2011-11-24 11:32:15.211 NSOperationQueueTest[3038:12d0b] task 1 is begin run …
    2011-11-24 11:32:25.215 NSOperationQueueTest[3038:12d0b] task 1 is run finished.
    2011-11-24 11:32:25.225 NSOperationQueueTest[3038:12d0b] task 2 is begin run …
    2011-11-24 11:32:35.226 NSOperationQueueTest[3038:12d0b] task 2 is run finished.

  • 相关阅读:
    翻转整数
    无重复字符的最长子串
    字节问题
    django model中get()和filter()方法的区别
    leetcode 290 Word Pattern(map的应用)
    leetcode 130 Surrounded Regions(BFS)
    leetcode 27 Remove Element
    leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)
    leetcode 111 Minimum Depth of Binary Tree(DFS)
    leetcode 104 Maximum Depth of Binary Tree(DFS)
  • 原文地址:https://www.cnblogs.com/GnagWang/p/2261400.html
Copyright © 2020-2023  润新知