多線程之NSThread

NSThread的使用:

// 1. 使用類方法創建線程并自動啟動線程
    [NSThread detachNewThreadSelector:@selector(ThreadMethod) toTarget:self withObject:nil];
    [NSThread detachNewThreadWithBlock:^{
        // run ...
        [self ThreadMethod];
    }];

// 2. init創建并啟動
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(ThreadMethod) object:nil];
    // 設置線程名
    [thread setName:@"NSThread"];
    // 設置優先級,優先級從0到1,1最高
    [thread setThreadPriority:0.9];
    // 啟動
    [thread start];

//3.隱式創建,直接啟動 
[self performSelectorInBackground:@selector(ThreadMethod:) withObject:@"NSThread3"];

全部API介紹

//獲取當前線程
+(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;

//線程字典
- (NSMutableDictionary *)threadDictionary;

//線程休眠到什么時間
+ (void)sleepUntilDate:(NSDate *)date;

//線程休眠多久
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

//退出線程
+ (void)exit;

//線程優先級
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;

- (double)threadPriority;
- (void)setThreadPriority:(double)threadPriority;

//調用棧返回地址
+ (NSArray *)callStackReturnAddresses NS_AVAILABLE(10_5, 2_0);
+ (NSArray *)callStackSymbols NS_AVAILABLE(10_6, 4_0);

//線程名字
- (NSString *)name;
- (void)setName:(nullable NSString *)name NS_AVAILABLE(10_5, 2_0);

//獲取棧的大小
- (NSUInteger)stackSize NS_AVAILABLE(10_5, 2_0);
- (void)setStackSize:(NSUInteger)s NS_AVAILABLE(10_5, 2_0);

//是否是主線程
- (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0);
+ (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0); // reports whether current thread is main

// 獲得主線程對象
+ (NSThread *)mainThread;

//實例化方法
- (id)init NS_AVAILABLE(10_5, 2_0); // designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (id)initWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

//是否正在執行
- (BOOL)isExecuting NS_AVAILABLE(10_5, 2_0);

//是否執行完成
- (BOOL)isFinished NS_AVAILABLE(10_5, 2_0);

//是否取消線程
- (BOOL)isCancelled NS_AVAILABLE(10_5, 2_0);

//取消線程
- (void)cancel NS_AVAILABLE(10_5, 2_0);

//線程啟動
- (void)start NS_AVAILABLE(10_5, 2_0);

- (void)main NS_AVAILABLE(10_5, 2_0); // thread body method


//多線程通知
FOUNDATION_EXPORT NSString * const NSWillBecomeMultiThreadedNotification;
FOUNDATION_EXPORT NSString * const NSDidBecomeSingleThreadedNotification;
FOUNDATION_EXPORT NSString * const NSThreadWillExitNotification;


//與主線程通信
- (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 API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
// equivalent to the first method with kCFRunLoopCommonModes


//隱式創建并啟動線程
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));




?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、簡介:1.1 iOS有三種多線程編程的技術,分別是:1.、NSThread2、Cocoa NSOperatio...
    LuckTime閱讀 1,374評論 0 1
  • iOS中的多線程技術主要有NSThread, GCD和NSOperation。他們的封裝層次依次遞增,其中 NST...
    RobinYu閱讀 178評論 0 0
  • NSThread 是蘋果官方提供的,使用起來比 pthread 更加面向對象,簡單易用,可以直接操作線程對象。不過...
    iOS猿_員閱讀 172評論 0 0
  • 開啟線程 分離主線程創建:創建線程后會自動執行,但是線程外部不可獲取到該線程對象detachNewThreadWi...
    Mr_Pt閱讀 1,094評論 0 1
  • 1. 線程的概念 首先簡單敘述一下這兩個概念,我們在電腦上單獨運行的每個程序就是一個獨立的進程,通常進程之間是相互...
    大成小棧閱讀 438評論 0 0