NSThread
一个NSThread对象就代表一条线程
创建、启动线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; // 线程一启动,就会在线程thread中执行self的run方法
[thread start];
主线程相关用法
+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程
其他用法
获得当前线程
NSThread *current = [NSThread currentThread];
线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他创建线程方式
创建线程后自动启动线程
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
隐式创建并启动线程
[self performSelectorInBackground:@selector(run) withObject:nil];
上述2种创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置,例如设置优先级
控制线程状态
启动线程
// 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态
- (void)start;
阻塞(暂停)线程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti; // 进入阻塞状态
强制停止线程
+ (void)exit; // 进入死亡状态
注意:一旦线程停止(死亡)了,就不能再次开启任务
多线程的安全隐患
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象、同一个变量、同一个文件,当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
安全隐患解决 – 互斥锁
互斥锁使用格式
@synchronized(锁对象) { // 需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步
线程同步的意思是:多条线程在同一条线上执行(按顺序地执行任务)
互斥锁,就是使用了线程同步技术
原子和非原子属性
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
原子和非原子属性的选择
nonatomic和atomic对比
atomic:线程安全,需要消耗大量的资源
nonatomic:非线程安全,适合内存小的移动设备
iOS开发的建议
所有属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力
线程间通信
什么叫做线程间通信?
在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信
线程间通信的体现?
1个线程传递数据给另1个线程,在1个线程中执行完特定任务后,转到另1个线程继续执行任务
线程间通信常用方法:
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
让子线程不死亡的办法
使用RunLoop
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSThread *thread; // 强引用子线程
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 启动一个子线程
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
self.thread.name = @"JYThread";
[self.thread start];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 多次点击时, 使用的子线程还是原来的子线程
[self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:YES];
}
- (void)run
{
// 在子线程里创建一个RunLoop, 并且加入一个Port到RunLoop
// 当RunLoop里面有Source、Timer、Observe中的任意一个, RunLoop就不会退出(Port属于Source中的一种)
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
// 运行RunLoop
[[NSRunLoop currentRunLoop] run];
NSLog(@"不会执行到这一行代码");
}
- (void)test
{
NSLog(@"test--- currentThread = %@", [NSThread currentThread]);
}
@end