iOS多线程demogit
iOS多线程之--NSThreadgithub
iOS多线程相关面试题多线程
NSThread是苹果官方提供面向对象操做线程的技术,简单方便,能够直接操做线程对象,不过须要本身控制线程的生命周期,平时开发中使用的并很少。post
NSThread
实例化对象有2中方式,一种是经过target
的方式执行任务,一种是经过block
的方式执行任务。另外还有2种隐式建立线程的方式。动画
- (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}
复制代码
- (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}
复制代码
- (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)}
复制代码
- (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)}
复制代码
@property (nullable, copy) NSString *name;
复制代码
给线程取个名字,只是方便调试时知道是哪一个线程,没有其它实际用途。ui
@property double threadPriority;
复制代码
线程优先级,取值范围0.0-1.0,1.0表示最高优先级,默认是0.5.spa
@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;
复制代码
线程是否已经取消。
@property (class, readonly, strong) NSThread *currentThread;
复制代码
当前线程。
+ (BOOL)isMultiThreaded;
复制代码
是不是多线程。
@property (class, readonly) BOOL isMainThread;
复制代码
当前线程是不是主线程。
@property (class, readonly, strong) NSThread *mainThread;
复制代码
主线程。
// 线程休眠到指定时间
+ (void)sleepUntilDate:(NSDate *)date;
// 线程休眠指定时长
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
// 退出线程
+ (void)exit;
// 是不是多线程
+ (BOOL)isMultiThreaded;
复制代码
- (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;
复制代码