一、什么是NSThread
NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程,
需要手动管理线程的生命周期,处理线程同步等问题。
二、NSThread方法介绍
1)动态创建
1 |
selector:@selector(threadRun) object:nil]; |
动态方法返回一个新的thread对象,需要调用start方法来启动线程
2)静态创建
1 |
|
由于静态方法没有返回值,如果需要获取新创建的thread,需要在selector中调用获取当前线程的方法
3)线程开启
1 |
|
4)线程暂停
1 2 |
|
NSThread的暂停会有阻塞当前线程的效果
5)线程取消
1 |
|
取消线程并不会马上停止并退出线程,仅仅只作(线程是否需要退出)状态记录
6)线程停止
1 |
|
停止方法会立即终止除主线程以外所有线程(无论是否在执行任务)并退出,需要在掌控所有线程状态的情况下调用此方法,
否则可能会导致内存问题。
7)获取当前线程
1 |
|
8)获取主线程
1 |
|
9)线程优先级设置
iOS 8以前使用
1 |
|
这个方法的优先级的数值设置让人困惑,因为你不知道你应该设置多大的值是比较合适的,因此在iOS8之后,threadPriority添加了
一句注释:To be deprecated; use qualityOfService below
意思就是iOS 8以后推荐使用qualityOfService属性,通过量化的优先级枚举值来设置
qualityOfService的枚举值如下:
- NSQualityOfServiceUserInteractive:最高优先级,用于用户交互事件
- NSQualityOfServiceUserInitiated:次高优先级,用于用户需要马上执行的事件
- NSQualityOfServiceDefault:默认优先级,主线程和没有设置优先级的线程都默认为这个优先级
- NSQualityOfServiceUtility:普通优先级,用于普通任务
- NSQualityOfServiceBackground:最低优先级,用于不重要的任务
比如给线程设置次高优先级:
1 |
|
三、线程间通信
常用的有三种:
1、指定当前线程执行操作
1 2 3 |
|
2、(在其他线程中)指定主线程执行操作
1 |
waitUntilDone:YES]; |
注意:更新UI要在主线程中进行
3、(在主线程中)指定其他线程执行操作
1 2 |
withObject:nil waitUntilDone:YES];
|
四、线程同步
线程和其他线程可能会共享一些资源,当多个线程同时读写同一份共享资源的时候,可能会引起冲突。线程同步是指是指在一定的时间内只允许
某一个线程访问某个资源
iOS实现线程加锁有NSLock和@synchronized两种方式。
五、线程的创建和使用实例:模拟售票
情景:某演唱会门票发售,在广州和北京均开设窗口进行销售,以下是代码实现
先监听线程退出的通知,以便知道线程什么时候退出
1 |
name:NSThreadWillExitNotification object:nil]; |
设置演唱会的门票数量
1 |
|
新建两个子线程(代表两个窗口同时销售门票)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
selector:@selector(saleTicket) object:nil];
selector:@selector(saleTicket) object:nil];
我们需要给它加一个循环
|
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{number = 3, name = 广州售票窗口} Will Exit
{number = 2, name = 北京售票窗口} Will Exit</nsthread: 0x7fdc91e24d60> </nsthread: 0x7fdc91e289f0> |
可以看到,票的销售过程中出现了剩余数量错乱的情况,这就是前面提到的线程同步问题。
售票是一个典型的需要线程同步的场景,由于售票渠道有很多,而票的资源是有限的,当多个渠道在短时间内卖出大量
的票的时候,如果没有同步机制来管理票的数量,将会导致票的总数和售出票数对应不上的错误。
我们在售票的过程中给票加上同步锁:同一时间内,只有一个线程能对票的数量进行操作,当操作完成之后,其他线程
才能继续对票的数量进行操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{number = 3, name = 广州售票窗口} Will Exit
{number = 2, name = 北京售票窗口} Will Exit</nsthread: 0x7ff0c1420cb0> </nsthread: 0x7ff0c1637320> |
可以看到,票的数量没有出现错乱的情况。
线程的持续运行和退出
我们注意到,线程启动后,执行saleTicket完毕后就马上退出了,怎样能让线程一直运行呢(窗口一直开放,
可以随时指派其卖演唱会的门票的任务),答案就是给线程加上runLoop
1 2 |
selector:@selector(threadExitNotice) name:NSThreadWillExitNotification object:nil]; |
1 2 |
|
新建两个子线程(代表两个窗口同时销售门票)
1 2 3 4 |
selector:@selector(thread1) object:nil];
selector:@selector(thread2) object:nil];
|
接着我们给线程创建一个runLoop
1 2 3 4 5 6 7 8 9 10 |
|
然后就可以指派任务给线程了,这里我们让两个线程都执行相同的任务(售票)
1 2 |
withObject:nil waitUntilDone:NO];
withObject:nil waitUntilDone:NO]; |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<nsthread: 0x7fe0d3c24360>{number = 3, name = 广州售票窗口} Will Exit</nsthread: 0x7fe0d3c24360> |
可以看到,当票卖完后,两个线程并没有退出,仍在继续运行,当到达指定时间后,线程2退出了,
如果需要让线程1退出,需要我们手动管理。
比如我们让线程完成任务(售票)后自行退出,可以这样操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<nsthread: 0x7fb719d54000>{number = 2, name = 北京售票窗口} Will Exit
<nsthread: 0x7fb719d552f0>{number = 3, name = 广州售票窗口} Will Exit</nsthread: 0x7fb719d552f0></nsthread: 0x7fb719d54000> |
如果确定两个线程都是isCancelled状态,可以调用[NSThread exit]方法来终止线程。
NSThread
- 一个NSThread对象就代表一条线程
- NSThread会在执行完任务函数是被自动收回
- 一些常用的函数
//创建线程
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector() object:nil];
//object存的是参数
//修改参数名
thread.name = @"我的多线程";
//获取主线程
NSThread *thread = [NSThread mainThread];
//创建线程并自动启动
[NSThread detachNewThreadSelector:@selector() toTarget:self withObject:nil];
//隐士创建
[self performSelectorInBackground:@selector() withObject:nil];
//获取当前线程
[NSThread currentThread]
//启动线程
- (void)start;
//阻塞(暂停)线程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
// 进入阻塞状态
//停止当前正在运行的进程
+ (void)exit;
// 进入死亡状态,一旦死亡则不能重启
资源共享
- 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
- 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
解决办法互斥锁
- 互斥锁使用格式
- @synchronized(锁对象) { // 需要锁定的代码 }
- 只用一把锁,多锁是无效的
- 互斥锁的优缺点
- 优点:能有效防止因多线程抢夺资源造成的数据安全问题
- 缺点:需要消耗大量的CPU资源
- 互斥锁的使用前提:多条线程抢夺同一块资源
- 互斥锁的示例代码
#import "ViewController.h"
@interface ViewController ()
/** 售票机1*/
@property (strong,nonatomic) NSThread *threadOne;
/** 售票机2*/
@property (strong,nonatomic) NSThread *threadTwo;
/** 售票机3*/
@property (strong,nonatomic) NSThread *threadThree;
/** 售票机4*/
@property (strong,nonatomic) NSThread *threadFour;
/** 售票机5*/
@property (strong,nonatomic) NSThread *threadFive;
/** 数量*/
@property (assign,nonatomic) NSInteger count;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建线程
self.threadOne = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"one"];
self.threadTwo = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"two"];
self.threadThree = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"three"];
self.threadFour = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"four"];
self.threadFive = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"five"];
self.count = 100;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//开始线程
[self.threadOne start];
[self.threadTwo start];
[self.threadThree start];
[self.threadFour start];
[self.threadFive start];
}
- (void)run:(NSString *)param
{
while (1) {
//self是锁对象
@synchronized(self)
{
if (self.count > 0) {
_count--;
NSLog(@"%zd-----%@",self.count,[NSThread currentThread]);
}
else
{
NSLog(@"卖完了----%@",[NSThread currentThread]);
break;
}
}
}
}
@end