iOS多線程 pthread、NSThread

系列文章:

多線程

多線程 pthread、NSThread

多線程 GCD

多線程 NSOperation

多線程運(yùn)用

Pthreads

這是一套在很多操作系統(tǒng)上都通用的多線程API, 基于 c語(yǔ)言 的框架

#import <pthread.h>

// 創(chuàng)建線程,并執(zhí)行任務(wù)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    pthread_t thread;
    //創(chuàng)建一個(gè)線程并自動(dòng)執(zhí)行
    pthread_create(&thread, NULL, start, NULL);
}

void *start(void *data) {
    NSLog(@"%@", [NSThread currentThread]);

    return NULL;
}

打印輸出

2015-07-27 23:57:21.689 testThread[10616:2644653] <NSThread: 0x7fbb48d33690>{number = 2, name = (null)}

你需要手動(dòng)處理線程的各個(gè)狀態(tài)的轉(zhuǎn)換即管理生命周期,比如,這段代碼雖然創(chuàng)建了一個(gè)線程,但并沒(méi)有銷毀。

NSThread

這套方案是經(jīng)過(guò)蘋(píng)果封裝后的,并且完全面向?qū)ο蟮?。所以你可以直接操控線程對(duì)象,非常直觀和方便。但是,它的生命周期還是需要我們手動(dòng)管理,所以這套方案也是偶爾用用,比如 [NSThread currentThread],它可以獲取當(dāng)前線程類,你就可以知道當(dāng)前線程的各種屬性,用于調(diào)試十分方便。下面來(lái)看看它的一些用法。

創(chuàng)建并啟動(dòng)

  • 先創(chuàng)建線程類,再啟動(dòng)

OBJECTIVE-C

 // 創(chuàng)建
  NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];

  // 啟動(dòng) 線程一啟動(dòng),就會(huì)在線程thread中執(zhí)行self的run方法
  [thread start];

SWIFT

//創(chuàng)建
  let thread = NSThread(target: self, selector: "run:", object: nil)

  //啟動(dòng)
  thread.start()
  • 創(chuàng)建并自動(dòng)啟動(dòng)線程

OBJECTIVE-C

[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];

SWIFT

NSThread.detachNewThreadSelector("run:", toTarget: self, withObject: nil)
  • 使用 NSObject 的方法創(chuàng)建(隱式創(chuàng)建)并自動(dòng)啟動(dòng)線程

OBJECTIVE-C

  [self performSelectorInBackground:@selector(run:) withObject:nil];

SWIFT

蘋(píng)果認(rèn)為 performSelector: 不安全,所以在 Swift 去掉了這個(gè)方法。
Note: The performSelector: method and related selector-invoking methods are not imported in Swift because they are inherently unsafe.

上述2種創(chuàng)建線程方式的優(yōu)缺點(diǎn)

  • 優(yōu)點(diǎn):簡(jiǎn)單快捷
  • 缺點(diǎn):無(wú)法對(duì)線程進(jìn)行更詳細(xì)的設(shè)置

其他方法

//取消線程
- (void)cancel;

//啟動(dòng)線程
- (void)start;

//判斷某個(gè)線程的狀態(tài)的屬性
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled;

//設(shè)置和獲取線程名字
-(void)setName:(NSString *)n;
-(NSString *)name;

//獲取當(dāng)前線程信息
+ (NSThread *)currentThread;

//獲取主線程信息
+ (NSThread *)mainThread;

// 是否為主線程(類方法)
+ (BOOL)isMainThread;

// 是否為主線程(對(duì)象方法)
- (BOOL)isMainThread;

//使當(dāng)前線程暫停一段時(shí)間,或者暫停到某個(gè)時(shí)刻
+ (void)sleepForTimeInterval:(NSTimeInterval)time;
+ (void)sleepUntilDate:(NSDate *)date;

// 強(qiáng)制停止線程-> 進(jìn)入死亡狀態(tài)
+ (void)exit;
//注意:一旦線程停止(死亡)了,就不能再次開(kāi)啟任務(wù)

更詳盡的博客:

iOS多線程:『pthread、NSThread』詳盡總結(jié)

用來(lái)介紹 iOS 多線程中,pthread、NSThread 的使用方法及實(shí)現(xiàn)。

第一部分:pthread 的使用、其他相關(guān)方法。

第二部分:NSThread 的使用、線程相關(guān)用法、線程狀態(tài)控制方法、線程之間的通信、線程安全和線程同步,以及線程的狀態(tài)轉(zhuǎn)換相關(guān)知識(shí)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容