zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    修改Cosbench源码 支持s3的 http range request 测试场景
    CEPH s3 java sdk PUT对象并在同一个PUT请求中同时设置ACL为 Public
    庆祝团队合著的《自主实现SDN虚拟网络与企业私有云》终于得以出版 --- 本人负责分布式存储部分的编写
    Cosbench测试 RGW S3 path_style_access=true模式支持
    RGW 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    CEPH 使用SSD日志盘+SATA数据盘, 随OSD数目递增对性能影响的递增测试
    使用CEPH RGW admin ops API 进行用户user AK/SK管理的秘诀
    prometheus consul docker redis_exporter 自动注册配置
    Consul 使用手册(感觉比较全了)
    RDS for MySQL权限问题(错误代码:1227,1725)
  • 原文地址:https://www.cnblogs.com/GnagWang/p/2261400.html
Copyright © 2011-2022 走看看