多线程学习笔记 - NSThread
NSThread的集中创建方法
1.方法一: NSThread对象创建 需要手动开启线程
// 线程创建
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(test_1) object:@"对象线程方法"];
// 开启线程
[thread start];
// 方法
- (void) test_1
{
NSLog(@"test_1 - %@",[NSThread currentThread]);
//NSThread练习[1608:289527] test_1 - <NSThread: 0x600000262c40>{number = 3, name = (null)}
}
2.方法二: 分离子线程, 会自动启动线程
// 线程创建
[NSThread detachNewThreadSelector:@selector(test_2) toTarget:self withObject:@"分离线程"];
// 方法
- (void) test_2
{
NSLog(@"test_2 - %@",[NSThread currentThread]);
//NSThread练习[1608:289528] test_2 - <NSThread: 0x600000263640>{number = 4, name = (null)}
}
3.方法三: 开启一条后台线程, 也会自动启动线程
// 线程创建
[self performSelectorInBackground:@selector(test_3) withObject:@"后台程序"];
// 方法
- (void) test_3
{
NSLog(@"test_3 - %@",[NSThread currentThread]);
//2017-06-22 13:55:50.292 NSThread练习[1608:289529] test_3 - <NSThread: 0x600000263800>{number = 5, name = (null)}
}
方法对比
方法一
- 优点: 可以拿到线程对象, 并设置相关属性
- 缺点: 代码量相对多一点, 需要手动启动线程
方法二和方法三
- 优点: 创建线程简单快捷
- 缺点: 无法拿到线程对象, 无法设置相关属性
NSThread的线程名字name
与优先级threadPriority
threadPriority 的取值范围是 0.0 -- 1.0, 默认是0.5. 数值越大, 优先级越高 ,优先级越高,处理器先处理这个线程的概率越大。
给线程添加名字与优先级
NSThread * thread_1 = [[NSThread alloc] initWithTarget:self selector:@selector(compareTest) object:nil];
thread_1.name = @"线程——1"; // 线程名字
thread_1.threadPriority = 0.1; // 线程优先级
[thread_1 start];
NSThread * thread_2 = [[NSThread alloc] initWithTarget:self selector:@selector(compareTest) object:nil];
thread_2.name = @"线程——2";
thread_2.threadPriority = 0.5;
[thread_2 start];
NSThread * thread_3 = [[NSThread alloc] initWithTarget:self selector:@selector(compareTest) object:nil];
thread_3.name = @"线程——3";
thread_3.threadPriority = 1.0;
[thread_3 start];
打印出线程的名字与顺序分别是
// 优先级高的线程先执行
2017-06-22 14:19:00.461 NSThread练习[1690:379110] 当前线程名字 - 线程——3
2017-06-22 14:19:00.461 NSThread练习[1690:379109] 当前线程名字 - 线程——2
2017-06-22 14:19:00.461 NSThread练习[1690:379108] 当前线程名字 - 线程——1
控制线程
- (void)start; // 开启线程
+ (void)exit; // 结束线程
// 阻塞(或休眠)线程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
线程一旦进入到死亡状态, 线程也就停止了, 就不能再次启动任务.
线程安全
1.一块资源可能会被多个线程共享, 也就是多个线程可能会访问同一块资源
2.比如多个线程访问同一个对象, 同一个变量, 同一个文件
3.当多个线程访问同一块资源时, 很容易引发数据错乱和数据安全问题
卖票的多线程问题(多线程共享一块资源)
- 创建不同线程与票总数
// 总共100张票
self.ticketCount = 100;
NSThread * thread_A = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
NSThread * thread_B = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
NSThread * thread_C = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
thread_A.name = @"售票员A";
thread_B.name = @"售票员B";
thread_C.name = @"售票员C";
[thread_A start];
[thread_B start];
[thread_C start];
- 不使用线程锁的方法
// 卖票
- (void) saleTicket
{
while (1) {
NSInteger count = self.ticketCount;
if (count > 0)
{
for (int i = 0; i < 1000000; ++i)
{
// 只是耗时间, 没有其他用
}
self.ticketCount = count - 1;
NSLog(@"%@卖出一张票,还剩- %ld", [NSThread currentThread].name, (long)self.ticketCount);
} else {
NSLog(@"票卖完了");
break;
}
}
}
可以看出最后的票卖的很混乱
2017-06-22 15:15:55.212 NSThread练习[1968:555811] 售票员C卖出一张票,还剩- 2
2017-06-22 15:15:55.213 NSThread练习[1968:555810] 售票员B卖出一张票,还剩- 4
2017-06-22 15:15:55.214 NSThread练习[1968:555809] 售票员A卖出一张票,还剩- 2
2017-06-22 15:15:55.216 NSThread练习[1968:555811] 售票员C卖出一张票,还剩- 1
2017-06-22 15:15:55.217 NSThread练习[1968:555810] 售票员B卖出一张票,还剩- 3
2017-06-22 15:15:55.218 NSThread练习[1968:555809] 售票员A卖出一张票,还剩- 1
2017-06-22 15:15:55.221 NSThread练习[1968:555811] 售票员C卖出一张票,还剩- 0
2017-06-22 15:15:55.221 NSThread练习[1968:555811] 票卖完了
2017-06-22 15:15:55.222 NSThread练习[1968:555809] 售票员A卖出一张票,还剩- 0
2017-06-22 15:15:55.222 NSThread练习[1968:555809] 票卖完了
2017-06-22 15:15:55.223 NSThread练习[1968:555810] 售票员B卖出一张票,还剩- 2
2017-06-22 15:15:55.226 NSThread练习[1968:555810] 售票员B卖出一张票,还剩- 1
2017-06-22 15:15:55.229 NSThread练习[1968:555810] 售票员B卖出一张票,还剩- 0
2017-06-22 15:15:55.230 NSThread练习[1968:555810] 票卖完了
- 使用线程锁保证线程安全
// 卖票
- (void) saleTicket
{
while (1) {
// 加 互斥锁, 全局唯一, self, 代表锁对象
@synchronized(self)
{
NSInteger count = self.ticketCount;
if (count > 0)
{
self.ticketCount = count - 1;
NSLog(@"%@卖出一张票,还剩- %ld", [NSThread currentThread].name, (long)self.ticketCount);
} else {
NSLog(@"票卖完了");
break;
}
}
}
}
//不会出现数据混乱的情况了, 也达到了三个线程卖票的功能.
2017-06-22 15:24:00.116 NSThread练习[1995:594897] 售票员A卖出一张票,还剩- 5
2017-06-22 15:24:00.117 NSThread练习[1995:594899] 售票员C卖出一张票,还剩- 4
2017-06-22 15:24:00.117 NSThread练习[1995:594898] 售票员B卖出一张票,还剩- 3
2017-06-22 15:24:00.117 NSThread练习[1995:594897] 售票员A卖出一张票,还剩- 2
2017-06-22 15:24:00.117 NSThread练习[1995:594899] 售票员C卖出一张票,还剩- 1
2017-06-22 15:24:00.118 NSThread练习[1995:594898] 售票员B卖出一张票,还剩- 0
2017-06-22 15:24:00.119 NSThread练习[1995:594897] 票卖完了
2017-06-22 15:24:00.119 NSThread练习[1995:594899] 票卖完了
2017-06-22 15:24:00.120 NSThread练习[1995:594898] 票卖完了