时间:2021-05-19
那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了。
上代码
声明变量
@interface ViewController ()@property (strong, nonatomic)NSThread *thread1;@property (strong, nonatomic)NSThread *thread2;@property (strong, nonatomic)NSThread *thread3;@property (assign, nonatomic)int leftTickets;@end实现代码
- (void)viewDidLoad { [super viewDidLoad]; self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:nil]; self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:nil]; self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:nil]; self.thread1.name = @"thread1"; self.thread2.name = @"thread2"; self.thread3.name = @"thread3"; // 总票数 self.leftTickets = 10;}// 点击屏幕开启线程- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.thread1 start]; [self.thread2 start]; [self.thread3 start];}- (void)sellTickets { while (1) { @synchronized (self) { if (self.leftTickets > 0) { [NSThread sleepForTimeInterval:0.2]; int count = self.leftTickets; self.leftTickets = count - 1; NSLog(@"剩余的票数%d",self.leftTickets); NSLog(@"当前线程=%@", [NSThread currentThread]); }else { NSLog(@"票卖完了"); NSLog(@"退出线程%@",[NSThread currentThread]); [NSThread exit]; } } }}打印日志
2016-11-04 11:52:25.117 TTTTTTTTTT[6753:74162] 剩余的票数9
2016-11-04 11:52:25.117 TTTTTTTTTT[6753:74162] 当前线程=<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:25.393 TTTTTTTTTT[6753:74163] 剩余的票数8
2016-11-04 11:52:25.393 TTTTTTTTTT[6753:74163] 当前线程=<NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:25.661 TTTTTTTTTT[6753:74164] 剩余的票数7
2016-11-04 11:52:25.661 TTTTTTTTTT[6753:74164] 当前线程=<NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:25.932 TTTTTTTTTT[6753:74162] 剩余的票数6
2016-11-04 11:52:25.933 TTTTTTTTTT[6753:74162] 当前线程=<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.164 TTTTTTTTTT[6753:74163] 剩余的票数5
2016-11-04 11:52:26.165 TTTTTTTTTT[6753:74163] 当前线程=<NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:26.438 TTTTTTTTTT[6753:74164] 剩余的票数4
2016-11-04 11:52:26.439 TTTTTTTTTT[6753:74164] 当前线程=<NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:26.704 TTTTTTTTTT[6753:74162] 剩余的票数3
2016-11-04 11:52:26.705 TTTTTTTTTT[6753:74162] 当前线程=<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.975 TTTTTTTTTT[6753:74163] 剩余的票数2
2016-11-04 11:52:26.976 TTTTTTTTTT[6753:74163] 当前线程=<NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:27.232 TTTTTTTTTT[6753:74164] 剩余的票数1
2016-11-04 11:52:27.233 TTTTTTTTTT[6753:74164] 当前线程=<NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:27.505 TTTTTTTTTT[6753:74162] 剩余的票数0
2016-11-04 11:52:27.505 TTTTTTTTTT[6753:74162] 当前线程=<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:27.505 TTTTTTTTTT[6753:74163] 票卖完了
2016-11-04 11:52:27.506 TTTTTTTTTT[6753:74163] 退出线程<NSThread: x608000074540>{number = 4, name = thread2}
我们一般用@synchronized来给线程加锁。它有什么用呢:
(1) 堵塞所在线程,线程里面剩下的任务只有当@synchronized里面的代码执行完毕才能继续往下执行,和队列的同步差不多是一个意思。
(2)当执行@synchronized里面的代码之前,所在线程要先检查是否有其他的线程执行里面的代码。如果没有,才继续往下执行。
再看打印日志里面最后一条,说明了只有线程“thread3”退出了,其他的线程没有退出。
我上篇文章讲,不用管线程的退出,任务执行完线程会自动退出。但是这是一个while循环啊!如果不退出线程,线程会一直执行。
打印的日志
2016-11-04 12:01:53.309 TTTTTTTTTT[7110:78974] 当前线程=<NSThread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.556 TTTTTTTTTT[7110:78973] 剩余的票数0
2016-11-04 12:01:53.556 TTTTTTTTTT[7110:78973] 当前线程=<NSThread: x600000076fc0>{number = 3, name = thread1}
2016-11-04 12:01:53.556 TTTTTTTTTT[7110:78975] 票卖完了
2016-11-04 12:01:53.557 TTTTTTTTTT[7110:78975] 退出线程<NSThread: x600000077240>{number = 5, name = thread3}
2016-11-04 12:01:53.558 TTTTTTTTTT[7110:78974] 票卖完了
2016-11-04 12:01:53.559 TTTTTTTTTT[7110:78974] 退出线程<NSThread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.560 TTTTTTTTTT[7110:78973] 票卖完了
那又为什么只有线程thread2退出呢?(注:每次退出的线程是不确定的)因为当线程thread2退出了,并没有执行完@synchronized里的方法,线程thread1和线程thread3还在等thread2执行完了,它们好去执行呢。但是线程thread2已经死了,不可能再执行了。这就造成线程thread1和线程thread3一直都在内存里,没有被退出,造成了CPU不必要的开销,所以我们最好不要在@synchronized里面退出线程。
- (void)sellTickets { while (1) { @synchronized (self) { if (self.leftTickets > 0) { [NSThread sleepForTimeInterval:0.2]; int count = self.leftTickets; self.leftTickets = count - 1; NSLog(@"剩余的票数%d",self.leftTickets); NSLog(@"当前线程=%@", [NSThread currentThread]); } } if (self.leftTickets == 0) { NSLog(@"票卖完了"); NSLog(@"退出线程%@",[NSThread currentThread]); [NSThread exit]; } }}2016-11-04 12:06:51.795 TTTTTTTTTT[7295:81206] 票卖完了
2016-11-04 12:06:51.795 TTTTTTTTTT[7295:81207] 票卖完了
2016-11-04 12:06:51.795 TTTTTTTTTT[7295:81208] 票卖完了
2016-11-04 12:06:51.796 TTTTTTTTTT[7295:81206] 退出线程<NSThread: x60000026a3c0>{number = 3, name = thread1}
2016-11-04 12:06:51.796 TTTTTTTTTT[7295:81207] 退出线程<NSThread: x60000026a380>{number = 4, name = thread2}
2016-11-04 12:06:51.796 TTTTTTTTTT[7295:81208] 退出线程<NSThread: x60000026a740>{number = 5, name = thread3}
这就是NSThread加锁以及加锁的一些注意事项。如果感觉对你有用,记得关注啊,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
IOS多线程编程NSThread的使用方法NSThread是多线程的一种,有两种方法创建子线程(1)优点:NSThread比GCD、NSOperation都轻量
引言在多线程中,为了使线程安全,我们经常会使用synchronized和Lock进行代码同步和加锁,但是具体两者有什么区别,什么场景下适合用什么可能还不大清楚,
前言在单机应用时代,我们对一个共享的对象进行多线程访问的时候,使用java的synchronized关键字或者ReentrantLock类对操作的对象加锁就可以
在iOS开发中,多线程的实现方式主要有三种,NSThread、NSOperation和GCD,我前面博客中对NSOperation和GCD有了较为详细的实现
1.为什么要使用synchronized在并发编程中存在线程安全问题,主要原因有:1.存在共享数据2.多线程共同操作共享数据。关键字synchronized可以