那什麼時候需要加鎖呢,就是當多條線程同時操作一個變量時,就需要加鎖了。
上代碼
聲明變量
@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循環啊!如果不退出線程,線程會一直執行。
- (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 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加鎖以及加鎖的一些注意事項。如果感覺對你有用,記得關注啊,也希望大家多多支持本站。