iOS进阶之面试题

iOS 多线程 ------ NSThread 篇

2019-02-16  本文已影响25人  JimmyL

NSThread是苹果官方提供面向对象操作线程的技术,是Foundation框架提供的最基础的多线程类,简单方便,可以直接操作线程对象,不过需要自己控制线程的生命周期。在平时使用很少,最常用到的是 [NSThread currentThread] 获取当前线程。

实例初始化

- (void)viewDidLoad {
    [super viewDidLoad];
    //iOS 10 之前提供的实例创建方法
    NSThread *thread1 = [[NSThread alloc] init];
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction) object:nil];
        
    //iOS 10 之后增加的实例创建方法
    if (@available(iOS 10.0, *)) {
       NSThread *thread3 = [[NSThread alloc] initWithBlock:^{
           NSLog(@"3");
       }];
       [thread3 start];
    }

    thread1.name = @"test-01";
    thread2.name = @"test-02";
    [thread1 start];
    [thread2 start];
    
    NSLog(@"5");
}

实例方法及属性

//这个方法会将正在执行的当前进程信息保存给接收者,然后再将进程取消,同时会通过方法 isCancled 反馈状态,如果成功取消,isCancled将会返回YES,否则返回NO;
- (void)cancel;

//实例会在 start 后,将线程放进可调度线程池,等待被 CPU 适时调度,并在执行完 block 或 selector 会自动调用 exit 方法退出线程;
- (void)start;

//
- (void)main;

/**
 线程优先级

 - NSQualityOfServiceUserInteractive: 最高优先级,主要用于提供交互UI的操作,比如处理点击事件,绘制图像到屏幕上
 - NSQualityOfServiceUserInitiated: 次高优先级,主要用于执行需要立即返回的任务
 - NSQualityOfServiceUtility: 默认优先级,当没有设置优先级的时候,线程默认优先级
 - NSQualityOfServiceBackground: 普通优先级,主要用于不需要立即返回的任务
 - NSQualityOfServiceDefault: 后台优先级,用于完全不紧急的任务
 */
typedef NS_ENUM(NSInteger, NSQualityOfService) {
    NSQualityOfServiceUserInteractive = 0x21,
    NSQualityOfServiceUserInitiated = 0x19,
    NSQualityOfServiceUtility = 0x11,
    NSQualityOfServiceBackground = 0x09,
    NSQualityOfServiceDefault = -1
};

//线程优先级
@property NSQualityOfService qualityOfService;

//线程名字,便于调试
@property (nullable, copy) NSString *name;

//当前线程是否为主线程
@property (class, readonly) BOOL isMainThread;

//获取主线程
@property (class, readonly, strong) NSThread *mainThread;

//获取当前线程
@property (class, readonly, strong) NSThread *currentThread;

//线程是否正在运行
@property (readonly, getter=isExecuting) BOOL executing;

//线程是否完成
@property (readonly, getter=isFinished) BOOL finished;

//线程是否取消
@property (readonly, getter=isCancelled) BOOL cancelled;

注意:部分线程属性需要在启动前设置,线程启动之后再设置会无效

类方法

//直接开启子线程执行 block,然后退出此线程
+ (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

//直接开启子线程执行 selector,然后退出此线程
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;

//当前线程是否为主线程
+ (BOOL)isMultiThreaded;

//使当前线程休眠至指定 date 或指定时长
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

//退出当前线程
+ (void)exit;

类方法开启线程时由于 block 及 selector 无线程参数或返回值,所以若要获取当前线程需要通过 [NSThread currentThread] 获取

线程通知

FOUNDATION_EXPORT NSNotificationName const NSWillBecomeMultiThreadedNotification;
FOUNDATION_EXPORT NSNotificationName const NSDidBecomeSingleThreadedNotification;
FOUNDATION_EXPORT NSNotificationName const NSThreadWillExitNotification;

线程间通讯

以下方法位于 NSObject (NSThreadPerformAdditions) 分类中,所有继承 NSObject 实例化对象都可调用以下方法

/**
  指定方法在主线程中执行
aSelector: SEL 方法
arg: 方法参数
wait: 是否等待当前执行完毕, 当这个参数为YES,时表示当前循环中的时间马上响应这个事件,如果为NO则 runloop 会将这个事件加入 runloop 队列在合适的时间执行这个事件
modes array: 指定的Runloop model
*/
- (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
/**
  指定方法在某个线程中执行
  aSelector: SEL 方法
  arg: 方法参数
  wait: 是否等待当前执行完毕, 当这个参数为YES,时表示当前循环中的时间马上响应这个事件,如果为NO则 runloop 会将这个事件加入 runloop 队列在合适的时间执行这个事件
  modes array: 指定的Runloop model
*/
- (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
/**
  指定方法在开启的子线程中执行
  aSelector: SEL 方法
  arg: 方法参数
*/
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

生命周期

生命周期.png
上一篇下一篇

猜你喜欢

热点阅读