美文网首页
多线程之NSThread

多线程之NSThread

作者: Mr_Pt | 来源:发表于2017-05-10 10:54 被阅读81次

    开启线程

    • 分离主线程创建:
      创建线程后会自动执行,但是线程外部不可获取到该线程对象
      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
    }];
    
    • 构造方法创建:
      可拿到该线程对象,线程执行需手动启动

      - (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生命周期

    • 创建后开始执行:start
    - (void)start NS_AVAILABLE(10_5, 2_0);
    
    • 执行任务:main
      main方法为线程内执行的主要方法,NSThread的子类需重写该方法,加入任务代码
    - (void)main NS_AVAILABLE(10_5, 2_0);
    
    • 取消:cancel
    - (void)cancel NS_AVAILABLE(10_5, 2_0);
    
    • 终止线程:exit
      exit是类方法,表示终止当前线程,且不可恢复
    + (void)exit;
    
    • 一些属性
      • isExecuting:是否正在执行
        @property (readonly, getter=isExecuting) BOOL executing NS_AVAILABLE(10_5, 2_0);
        
      • isFinished:是否结束
        @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类别方法调用

    • 回归主线程
      在主线程中运行方法,wait表示是否阻塞这个方法的调用,如果为YES则等待主线程中运行方法结束。一般可用于在子线程中调用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;
    
    • 在指定线程中执行
      在指定线程中执行,但该线程必须具备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的其它一些常用的属性和方法

    • 获得当前线程
    @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);
    
    • 当前线程是否主线程
    @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);
    
    • 线程优先级
    + (double)threadPriority;
    + (BOOL)setThreadPriority:(double)p;
    @property double threadPriority NS_AVAILABLE(10_6, 4_0); // To be deprecated; use qualityOfService below
    
    • 服务质量:NSQualityOfService
      从iOS8开始,苹果框架概念基础中将不再过分强调线程这个概念。新的qualityOfService属性替换了ThreadPriority。这些新的语义允许应用程序推迟非关键工作,以确保始终如一的用户体验。
    @property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0); // read-only after the thread is started
    
    typedef NS_ENUM(NSInteger, NSQualityOfService) {
        // 与用户交互的任务,这些任务通常跟UI级别的刷新相关,比如动画,这些任务需要在一瞬间完成
        NSQualityOfServiceUserInteractive = 0x21,
        // 在实现用户精确请求请求相关工作时使用UserInitiated QoS,但不要求精确到毫秒,比如动画。例如,如果用户打开email app马上查看邮件。
        NSQualityOfServiceUserInitiated = 0x19,
        // Utility QoS用于执行已经由用户请求自动发生的任务。例如,电子邮件应用程序可以被配置为每隔5分钟自动检查邮件。如果系统是非常有限的资源,而电子邮件检查被推迟几分钟这也是被允许的。
        NSQualityOfServiceUtility = 0x11,
        // Background QoS用于执行用户可能甚至都没有意识到正在发生的工作,比如email app可能使用它来执行索引搜索
        NSQualityOfServiceBackground = 0x09,
        // 优先级介于user-initiated 和 utility,当没有 QoS信息时默认使用,开发者不应该使用这个值来设置自己的任务
        NSQualityOfServiceDefault = -1
    } NS_ENUM_AVAILABLE(10_10, 8_0);
    
    • 线程数据:threadDictionary
    @property (readonly, retain) NSMutableDictionary *threadDictionary;
    
    1. 指定线程的栈空间:stackSize属性,值是4KB及其倍数,最大512KB,主线程最大1MB
    @property NSUInteger stackSize NS_AVAILABLE(10_5, 2_0);
    

    一些非线程调用(NSObject的NSThreadPerformAdditions类别方法)

    • 在当前线程立即执行:注意它们会阻塞当前线程(包括UI线程):
    - (id)performSelector:(SEL)aSelector;
    - (id)performSelector:(SEL)aSelector withObject:(id)object;
    - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
    
    • 在当前线程延迟执行:
    - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
    - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
    

    如果当前线程没有显式使用NSRunLoop或已退出就无法执行了,需要注意,它们可以被下列方法终止:

    + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument;
    + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
    

    相关文章

      网友评论

          本文标题:多线程之NSThread

          本文链接:https://www.haomeiwen.com/subject/qlwwtxtx.html