多线程之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,383评论 0 1
  • iOS中的多线程技术主要有NSThread, GCD和NSOperation。他们的封装层次依次递增,其中 NST...
    RobinYu阅读 179评论 0 0
  • NSThread 是苹果官方提供的,使用起来比 pthread 更加面向对象,简单易用,可以直接操作线程对象。不过...
    iOS猿_员阅读 172评论 0 0
  • 开启线程 分离主线程创建:创建线程后会自动执行,但是线程外部不可获取到该线程对象detachNewThreadWi...
    Mr_Pt阅读 1,098评论 0 1
  • 1. 线程的概念 首先简单叙述一下这两个概念,我们在电脑上单独运行的每个程序就是一个独立的进程,通常进程之间是相互...
    大成小栈阅读 439评论 0 0