iOSマルチスレッドの簡単な使い方

4042 ワード

マルチスレッドを実現する方法は一般的にNSThread、GCD、NSOperationの3種類がある.
1、NSThread:適用が簡単で、簡単で使いやすくて、直接スレッドを操作することができて、ocの、たまに使うことができて、プログラマーはライフサイクルを管理します
//1.1     
//    :            
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
//demo
NSThread* myThread = [[NSThread alloc] initWithTarget:self  
                                        selector:@selector(doSomething:)  
                                        object:nil];  
[myThread start];

//   :           
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument;
//demo
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];  

//       UI
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];

//1.2  
NSLock *theLock = [[NSLock alloc] init];  
//  
[theLock lock];
//  
[theLock unlock];  

2、GCD:NSThreadなどのマルチスレッド技術に取って代わって、設備のマルチコア技術を十分に利用して、cの、よく使う、システムはスレッドのライフサイクルを自動的に管理する
//1、     dispatch_async
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
    //        
    dispatch_async(dispatch_get_main_queue(), ^{  
        //       
    });  
});  

//2、dispatch_group_async   
//dispatch_group_async              ,              。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
dispatch_group_t group = dispatch_group_create();  
dispatch_group_async(group, queue, ^{  
    [NSThread sleepForTimeInterval:1];  
    NSLog(@"group1");  
});  
dispatch_group_async(group, queue, ^{  
    [NSThread sleepForTimeInterval:2];  
    NSLog(@"group2");  
});  
dispatch_group_async(group, queue, ^{  
    [NSThread sleepForTimeInterval:3];  
    NSLog(@"group3");  
});  
dispatch_group_notify(group, dispatch_get_main_queue(), ^{  
    NSLog(@"updateUi");  
});  
dispatch_release(group); 

//          ,group    
dispatch_group_notify(group, dispatch_get_main_queue(), ^{  
 
});  
//    

//3、dispatch_barrier_async   
//dispatch_barrier_async                ,                    
//demo

dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT);  
dispatch_async(queue, ^{  
    [NSThread sleepForTimeInterval:2];  
    NSLog(@"dispatch_async1");  
});  
dispatch_async(queue, ^{  
    [NSThread sleepForTimeInterval:4];  
    NSLog(@"dispatch_async2");  
});  
dispatch_barrier_async(queue, ^{  
    NSLog(@"dispatch_barrier_async");  
    [NSThread sleepForTimeInterval:4];  
  
});  
dispatch_async(queue, ^{  
    [NSThread sleepForTimeInterval:1];  
    NSLog(@"dispatch_async3");  
});

//    
//dispatch_async1
//dispatch_async2
//dispatch_barrier_async
//dispatch_async3

//4、dispatch_apply   
         。
dispatch_apply(5, globalQ, ^(size_t index) {
    //   5 
});

3、NSOperation:gcdのパッケージに基づいて、gcdより簡単で、更に対象に向いて、システムはスレッドのライフサイクルを自動的に管理して、よく使う
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];  
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];  
    [queue addOperation:operation];  
    // Do any additional setup after loading the view, typically from a nib.  
}  
  
-(void)downloadImage:(NSString *)url{  
    NSLog(@"url:%@", url);  
    NSURL *nsUrl = [NSURL URLWithString:url];  
    NSData *data = [[NSData alloc]initWithContentsOfURL:nsUrl];  
    UIImage * image = [[UIImage alloc]initWithData:data];  
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];  
}  
-(void)updateUI:(UIImage*) image{  
    self.imageView.image = image;  
} 

//n   NSInvocationOperatio  ,    NSOperationQueue 。      downloadImage  。      performSelectorOnMainThread     updateUI  。
//   NSOperationQueue           。     ,     
[queue setMaxConcurrentOperationCount:5];
//   -1,