實現(xiàn)多線程的方式一般有三種:NSThread、GCD、NSOperation
1、NSThread:適用簡單,簡單易用,可以直接操作線程,oc 的,偶爾使用,程序員管理生命周期
//1.1 創(chuàng)建線程
//實例方法:創(chuàng)建線程后需手動開啟線程
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
//demo
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(doSomething:)
object:nil];
[myThread start];
//類方法:創(chuàng)建完成后直接運行線程
+ (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 等多線程技術(shù),充分利用設(shè)備多核技術(shù),c的,經(jīng)常使用,系統(tǒng)自動管理線程生命周期
//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可以實現(xiàn)監(jiān)聽一組任務(wù)是否完成,完成后得到通知執(zhí)行其他的操作。
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才會執(zhí)行
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
});
//中的方法
//3、dispatch_barrier_async的使用
//dispatch_barrier_async是在前面的任務(wù)執(zhí)行結(jié)束后它才執(zhí)行,而且它后面的任務(wù)等它執(zhí)行完成之后才會執(zhí)行
//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");
});
//運行結(jié)果
//dispatch_async1
//dispatch_async2
//dispatch_barrier_async
//dispatch_async3
//4、dispatch_apply 使用
重復(fù)執(zhí)行同一段代碼。
dispatch_apply(5, globalQ, ^(size_t index) {
// 執(zhí)行5次
});
3、NSOperation:基于 gcd 的封裝,比 gcd 簡單,更加面向?qū)ο?,系統(tǒng)自動管理線程生命周期,經(jīng)常使用
- (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中。后臺線程執(zhí)行downloadImage方法。方法完成后用performSelectorOnMainThread執(zhí)行主線程updateUI方法。
//加入到NSOperationQueue的線程默認全部同時進行。若想做限制,可用此方法
[queue setMaxConcurrentOperationCount:5];
//默認為-1,即無限制