iOS多线程之--NSThread

1,086 阅读4分钟


iOS多线程demo

iOS多线程之--NSThread

iOS多线程之--GCD详解

iOS多线程之--NSOperation

iOS多线程之--线程安全(线程锁)

iOS多线程相关面试题



NSThread是苹果官方提供面向对象操作线程的技术,简单方便,可以直接操作线程对象,不过需要自己控制线程的生命周期,平时开发中使用的并不多。

1. 创建线程

NSThread实例化对象有2中方式,一种是通过target的方式执行任务,一种是通过block的方式执行任务。另外还有2种隐式创建线程的方式。

1.1 target方式实例化

- (void)threadInitTarget{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task1) object:nil];
    thread.name = @"thread1"; // 给线程取名
    thread.threadPriority = 0.3; // 设置线程优先级
    [thread start]; // 启动线程
}

- (void)task1{
    NSLog(@"task1--%@",[NSThread currentThread]);
}

// ***************打印结果***************
2019-12-31 15:43:16.889148+0800 MultithreadingDemo[42576:4668160] task1--<NSThread: 0x600002b17140>{number = 7, name = thread1}

1.2 block方式实例化

- (void)threadInitBlock{
    NSThread *thread = [[NSThread alloc] initWithBlock:^{
       NSLog(@"task2--%@",[NSThread currentThread]);
    }];
    thread.name = @"thread2";
    [thread start];
}

// ***************打印结果***************
2019-12-31 15:43:21.130273+0800 MultithreadingDemo[42576:4668204] task2--<NSThread: 0x600002beacc0>{number = 8, name = thread2}

1.3 target方式隐式创建线程

- (void)implicitThreadTarget{
    [NSThread detachNewThreadSelector:@selector(task3) toTarget:self withObject:nil];
}

- (void)task3{
    NSLog(@"task3--%@",[NSThread currentThread]);
}

// ***************打印结果***************
2019-12-31 16:24:31.760099+0800 MultithreadingDemo[55958:4841154] task3--<NSThread: 0x60000081dcc0>{number = 7, name = (null)}

1.4 block方式隐式创建线程

- (void)implicitThreadBlock{
    [NSThread detachNewThreadWithBlock:^{
        NSLog(@"task4--%@",[NSThread currentThread]);
    }];
}

// ***************打印结果***************
2019-12-31 16:24:34.696310+0800 MultithreadingDemo[55958:4841190] task4--<NSThread: 0x6000008fa7c0>{number = 8, name = (null)}

2. NSThread 常用属性介绍

2.1 实例属性

@property (nullable, copy) NSString *name;

给线程取个名字,只是方便调试时知道是哪个线程,没有其它实际用途。


@property double threadPriority;

线程优先级,取值范围0.0-1.0,1.0表示最高优先级,默认是0.5.


@property NSQualityOfService qualityOfService;

这是iOS8之后新提供的设置优先级的方式。是一个枚举类型:

  • NSQualityOfServiceUserInteractive:与用户交互的任务,这些任务通常跟UI级别的刷新相关,比如动画,这些任务需要在一瞬间完成。
  • NSQualityOfServiceUserInitiated:由用户发起的并且需要立即得到结果的任务,比如滑动scroll view时去加载数据用于后续cell的显示,这些任务通常跟后续的用户交互相关,在几秒或者更短的时间内完成。
  • NSQualityOfServiceUtility:一些可能需要花点时间的任务,这些任务不需要马上返回结果,比如下载的任务,这些任务可能花费几秒或者几分钟的时间。
  • NSQualityOfServiceBackground:这些任务对用户不可见,比如后台进行备份的操作,这些任务可能需要较长的时间,几分钟甚至几个小时。
  • NSQualityOfServiceDefault:优先级介于user-initiated 和 utility,当没有 QoS信息时默认使用,开发者不应该使用这个值来设置自己的任务。

@property NSUInteger stackSize;

线程的堆栈大小,线程执行前堆栈大小为512K,线程完成后堆栈大小为0K。注意线程执行完毕后,由于内存空间被释放,不能再次启动。


@property (readonly) BOOL isMainThread;

是否是主线程。


@property (readonly, getter=isExecuting) BOOL executing;

线程是否正在执行。


@property (readonly, getter=isFinished) BOOL finished;

线程是否执行完成。


@property (readonly, getter=isCancelled) BOOL cancelled;

线程是否已经取消。


2.2 类属性

@property (class, readonly, strong) NSThread *currentThread;

当前线程。


+ (BOOL)isMultiThreaded;

是否是多线程。


@property (class, readonly) BOOL isMainThread;

当前线程是否是主线程。


@property (class, readonly, strong) NSThread *mainThread;

主线程。


3. NSThread 的类方法

// 线程休眠到指定时间
+ (void)sleepUntilDate:(NSDate *)date;

// 线程休眠指定时长
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

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

// 是否是多线程
+ (BOOL)isMultiThreaded;

4. NSThread 其它相关方法

- (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;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg;