OC 多執行緒NSTheard

韋家冰發表於2017-12-13

####NSThread.h

@interface NSThread : NSObject
//當前執行緒
@property (class, readonly, strong) NSThread *currentThread;
//使用類方法建立執行緒執行任務
+ (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;
//判斷當前是否為多執行緒
+ (BOOL)isMultiThreaded;
//指定執行緒的執行緒引數,例如設定當前執行緒的斷言處理器。
@property (readonly, retain) NSMutableDictionary *threadDictionary;
//當前執行緒暫停到某個時間
+ (void)sleepUntilDate:(NSDate *)date;
//當前執行緒暫停一段時間
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
//退出當前執行緒
+ (void)exit;
//當前執行緒優先順序
+ (double)threadPriority;
//設定當前執行緒優先順序
+ (BOOL)setThreadPriority:(double)p;
//指定執行緒物件優先順序 0.0~1.0,預設值為0.5
@property double threadPriority NS_AVAILABLE(10_6, 4_0);
//服務質量
@property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0);
//執行緒名稱
@property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0);
//棧區大小
@property NSUInteger stackSize NS_AVAILABLE(10_5, 2_0);
//是否為主執行緒
@property (class, readonly) BOOL isMainThread NS_AVAILABLE(10_5, 2_0);
//獲取主執行緒
@property (class, readonly, strong) NSThread *mainThread NS_AVAILABLE(10_5, 2_0);
//初始化
- (instancetype)init NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
//例項方法初始化,需要再呼叫start方法
- (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));
//執行緒狀態,正在執行
@property (readonly, getter=isExecuting) BOOL executing NS_AVAILABLE(10_5, 2_0);
//執行緒狀態,正在完成
@property (readonly, getter=isFinished) BOOL finished NS_AVAILABLE(10_5, 2_0);
//執行緒狀態,已經取消
@property (readonly, getter=isCancelled) BOOL cancelled NS_AVAILABLE(10_5, 2_0);
//取消,僅僅改變執行緒狀態,並不能像exist一樣真正的終止執行緒
- (void)cancel NS_AVAILABLE(10_5, 2_0);
//開始
- (void)start NS_AVAILABLE(10_5, 2_0);
//執行緒需要執行的程式碼,一般寫子類的時候會用到
- (void)main NS_AVAILABLE(10_5, 2_0);
@end
複製程式碼

NSObject的分類各種performSelector方法

@interface NSObject (NSThreadPerformAdditions)
//隱式的建立並啟動執行緒,並在指定的執行緒(主執行緒或子執行緒)上執行方法。
- (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;
- (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);
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);
@end
複製程式碼

####一般用法

    //方法1:使用物件方法
    NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(startNewThread1) object:nil];
    //⚠️注意1:啟動一個執行緒並非就一定立即執行,而是處於就緒狀態,當CUP排程時才真正執行
    [thread1 start];
    
    //方法2:使用類方法,自動start
    [NSThread detachNewThreadSelector:@selector(startNewThread1) toTarget:self withObject:nil];
    
    //方法3:隱式建立並啟動執行緒
    [self performSelectorInBackground:@selector(run) withObject:nil];
複製程式碼

###解析注意 ######1.啟動一個執行緒並非就一定立即執行,而是處於就緒狀態,當CUP排程時才真正執行,如下圖

OC  多執行緒NSTheard

相關文章