iOS多線程開發(fā)——NSThread淺析
在iOS開發(fā)中,多線程的實(shí)現(xiàn)方式主要有三種,NSThread、NSOperation和GCD,我前面博客中對(duì)NSOperation和GCD有了較為詳細(xì)的實(shí)現(xiàn),為了學(xué)習(xí)的完整性,今天我們主要從代碼層面來實(shí)現(xiàn)NSThread的使用。案例代碼上傳至 https://github.com/chenyufeng1991/NSThread。
(1)初始化并啟動(dòng)一個(gè)線程
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//獲取當(dāng)前線程
NSThread *current = [NSThread currentThread];
NSLog(@"當(dāng)前線程為 %@",current);
//初始化線程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
//設(shè)置線程的優(yōu)先級(jí)(0.0-1.0)
thread.threadPriority = 1.0;
thread.name = @"新線程1";
[thread start];
}
- (void)run
{
NSLog(@"線程執(zhí)行");
//獲取當(dāng)前線程
NSThread *current = [NSThread currentThread];
NSLog(@"當(dāng)前線程為 %@",current);
//線程休眠,可以模擬耗時(shí)操作
[NSThread sleepForTimeInterval:2];
//獲取主線程
NSThread *mainThread = [NSThread mainThread];
NSLog(@"子線程中獲得主線程 %@",mainThread);
}
其中currentThread,這個(gè)方法很有用,常??梢杂脕砼袛嗄撤椒ǖ膱?zhí)行是在哪個(gè)線程中。
(2)NSThread可以指定讓某個(gè)線程在后臺(tái)執(zhí)行:
//后臺(tái)創(chuàng)建一個(gè)線程來執(zhí)行任務(wù),需要在調(diào)用的方法中使用自動(dòng)釋放池 [self performSelectorInBackground:@selector(run3) withObject:nil];
- (void)run3
{
@autoreleasepool {
NSLog(@"主線程3:%@,當(dāng)前線程3:%@",[NSThread mainThread],[NSThread currentThread]);
}
}
(3)子線程執(zhí)行耗時(shí)操作,主線程更新UI。這是多線程開發(fā)中最常用的案例。子線程中調(diào)用performSelectorOnMainThread方法用來更新主線程。
//測(cè)試在子線程中調(diào)用主線程更新UI
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSThread *subThread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
//NSThread可以控制線程開始
[subThread start];
}
- (void)run
{
NSLog(@"主線程1:%@,當(dāng)前線程1:%@",[NSThread mainThread],[NSThread currentThread]);
//以下方法需要在子線程中調(diào)用
[self performSelectorOnMainThread:@selector(invocationMainThread) withObject:nil waitUntilDone:YES];
}
- (void)invocationMainThread
{
NSLog(@"主線程2:%@,當(dāng)前線程2:%@",[NSThread mainThread],[NSThread currentThread]);
NSLog(@"調(diào)用主線程更新UI");
}
(4)同樣,我們也可以新建一個(gè)子線程的類,繼承自NSThread. 然后重寫里面的main方法,main方法就是該線程啟動(dòng)時(shí)會(huì)執(zhí)行的方法。
@implementation MyThread
- (void)main
{
NSLog(@"main方法執(zhí)行");
}
@end
然后按正常的創(chuàng)建啟動(dòng)即可。線程就會(huì)自動(dòng)去執(zhí)行main方法。
//可以自己寫一個(gè)子類,繼承自NSThread,需要重寫main方法
/**
* 執(zhí)行的代碼是在main中的,而不是使用@selector.
使用main方法,線程中執(zhí)行的方法是屬于對(duì)象本身的,這樣可以在任何其他需要使用這個(gè)線程方法的地方使用,而不用再一次實(shí)現(xiàn)某個(gè)方法。
而其他的直接NSThread的創(chuàng)建線程,線程內(nèi)執(zhí)行的方法都是在當(dāng)前的類文件里面的。
*/
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
MyThread *thread = [[MyThread alloc] init];
[thread start];
}
(5)NSThread中還有一個(gè)很常用的方法就是延遲。延遲2s執(zhí)行。
//線程休眠,可以模擬耗時(shí)操作 [NSThread sleepForTimeInterval:2];
對(duì)于多線程的三種實(shí)現(xiàn)方式,我們都要能夠熟練使用
相關(guān)文章
iOS開發(fā)實(shí)現(xiàn)下載器的基本功能(1)
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)下載器基本功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
iOS11上Xcode9 AppIcon 圖標(biāo)不顯示
升級(jí)到iOS11系統(tǒng)下自己的項(xiàng)目桌面app圖標(biāo)不見了,是什么原因呢,經(jīng)過一番查找,終于發(fā)現(xiàn)問題所在,現(xiàn)在分享給大家2017-11-11
Swift中的HTTP請(qǐng)求體Request Bodies使用示例詳解
這篇文章主要為大家介紹了Swift中的HTTP請(qǐng)求體Request Bodies使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
利用iOS手勢(shì)與scrollView代理實(shí)現(xiàn)圖片的放大縮小
這篇文章主要介紹了利用iOS的手勢(shì)、scrollView代理來實(shí)現(xiàn)圖片放大縮小的方法,文中通過示例代碼介紹的很詳細(xì),相信對(duì)各位iOS開發(fā)者們來說具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01

