iOS多线程的简单用法

实现多线程的方式一般有三种:NSThread、GCD、NSOperation

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,即无限制

你可能感兴趣的:(iOS多线程的简单用法)