多線程之NSThread

開啟線程

  • 分離主線程創(chuàng)建:
    創(chuàng)建線程后會自動執(zhí)行,但是線程外部不可獲取到該線程對象
    detachNewThreadWithBlock:
    detachNewThreadSelector:toTarget:withObject:
+ (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;

[NSThread detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument];
[NSThread detachNewThreadWithBlock:^{
    // do something
}];
  • 構(gòu)造方法創(chuàng)建:
    可拿到該線程對象,線程執(zhí)行需手動啟動

    - (instancetype)init NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
    - (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument NS_AVAILABLE(10_5, 2_0);
    - (instancetype)initWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
    
    NSThread *thread = [[NSThread alloc] initWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument];
    [thread start];
    
  • performSelector方法:NSObject的NSThreadPerformAdditions類別
    任何OC對象均可由下列方法開啟子線程或回到主線程

    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
    // equivalent to the first method with kCFRunLoopCommonModes
    
    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array NS_AVAILABLE(10_5, 2_0);
    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);
    // equivalent to the first method with kCFRunLoopCommonModes
    - (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);
    

NSThread生命周期

  • 創(chuàng)建后開始執(zhí)行:start
- (void)start NS_AVAILABLE(10_5, 2_0);
  • 執(zhí)行任務(wù):main
    main方法為線程內(nèi)執(zhí)行的主要方法,NSThread的子類需重寫該方法,加入任務(wù)代碼
- (void)main NS_AVAILABLE(10_5, 2_0);
  • 取消:cancel
- (void)cancel NS_AVAILABLE(10_5, 2_0);
  • 終止線程:exit
    exit是類方法,表示終止當(dāng)前線程,且不可恢復(fù)
+ (void)exit;
  • 一些屬性
    • isExecuting:是否正在執(zhí)行
      @property (readonly, getter=isExecuting) BOOL executing NS_AVAILABLE(10_5, 2_0);
      
    • isFinished:是否結(jié)束
      @property (readonly, getter=isFinished) BOOL finished NS_AVAILABLE(10_5, 2_0);
      
    • isCancelled:是否取消
      @property (readonly, getter=isCancelled) BOOL cancelled NS_AVAILABLE(10_5, 2_0);
      

線程間通訊:通過NSObject的NSThreadPerformAdditions類別方法調(diào)用

  • 回歸主線程
    在主線程中運行方法,wait表示是否阻塞這個方法的調(diào)用,如果為YES則等待主線程中運行方法結(jié)束。一般可用于在子線程中調(diào)用UI方法。
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
  • 在指定線程中執(zhí)行
    在指定線程中執(zhí)行,但該線程必須具備runloop(kCFRunLoopCommonModes)
// equivalent to the first method with kCFRunLoopCommonModes
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array NS_AVAILABLE(10_5, 2_0);
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);
  • Background
// equivalent to the first method with kCFRunLoopCommonModes
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);

NSThread的其它一些常用的屬性和方法

  • 獲得當(dāng)前線程
@property (class, readonly, strong) NSThread *currentThread;
  • 線程休眠
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
  1. 獲得主線程
@property (class, readonly, strong) NSThread *mainThread NS_AVAILABLE(10_5, 2_0);
  • 當(dāng)前線程是否主線程
@property (readonly) BOOL isMainThread NS_AVAILABLE(10_5, 2_0);
@property (class, readonly) BOOL isMainThread NS_AVAILABLE(10_5, 2_0);
  • 線程名
@property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0);
  • 線程優(yōu)先級
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
@property double threadPriority NS_AVAILABLE(10_6, 4_0); // To be deprecated; use qualityOfService below
  • 服務(wù)質(zhì)量:NSQualityOfService
    從iOS8開始,蘋果框架概念基礎(chǔ)中將不再過分強調(diào)線程這個概念。新的qualityOfService屬性替換了ThreadPriority。這些新的語義允許應(yīng)用程序推遲非關(guān)鍵工作,以確保始終如一的用戶體驗。
@property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0); // read-only after the thread is started

typedef NS_ENUM(NSInteger, NSQualityOfService) {
    // 與用戶交互的任務(wù),這些任務(wù)通常跟UI級別的刷新相關(guān),比如動畫,這些任務(wù)需要在一瞬間完成
    NSQualityOfServiceUserInteractive = 0x21,
    // 在實現(xiàn)用戶精確請求請求相關(guān)工作時使用UserInitiated QoS,但不要求精確到毫秒,比如動畫。例如,如果用戶打開email app馬上查看郵件。
    NSQualityOfServiceUserInitiated = 0x19,
    // Utility QoS用于執(zhí)行已經(jīng)由用戶請求自動發(fā)生的任務(wù)。例如,電子郵件應(yīng)用程序可以被配置為每隔5分鐘自動檢查郵件。如果系統(tǒng)是非常有限的資源,而電子郵件檢查被推遲幾分鐘這也是被允許的。
    NSQualityOfServiceUtility = 0x11,
    // Background QoS用于執(zhí)行用戶可能甚至都沒有意識到正在發(fā)生的工作,比如email app可能使用它來執(zhí)行索引搜索
    NSQualityOfServiceBackground = 0x09,
    // 優(yōu)先級介于user-initiated 和 utility,當(dāng)沒有 QoS信息時默認(rèn)使用,開發(fā)者不應(yīng)該使用這個值來設(shè)置自己的任務(wù)
    NSQualityOfServiceDefault = -1
} NS_ENUM_AVAILABLE(10_10, 8_0);
  • 線程數(shù)據(jù):threadDictionary
@property (readonly, retain) NSMutableDictionary *threadDictionary;
  1. 指定線程的??臻g:stackSize屬性,值是4KB及其倍數(shù),最大512KB,主線程最大1MB
@property NSUInteger stackSize NS_AVAILABLE(10_5, 2_0);

一些非線程調(diào)用(NSObject的NSThreadPerformAdditions類別方法)

  • 在當(dāng)前線程立即執(zhí)行:注意它們會阻塞當(dāng)前線程(包括UI線程):
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
  • 在當(dāng)前線程延遲執(zhí)行:
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;

如果當(dāng)前線程沒有顯式使用NSRunLoop或已退出就無法執(zhí)行了,需要注意,它們可以被下列方法終止:

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

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

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