When does it take to add a lock? It means that when multiple threads operate a variable at the same time, it needs to add a lock.
On the code
Declare variables
@interface ViewController () @property (strong, nonatomic)NSThread *thread1; @property (strong, nonatomic)NSThread *thread2; @property (strong, nonatomic)NSThread *thread3; @property (assign, nonatomic)int leftTickets; @end
Implement code
- (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"; self. = @"thread2"; self. = @"thread3"; // Total number of votes = 10; } // Click the screen to open the thread- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.thread1 start]; [self.thread2 start]; [self.thread3 start]; } - (void)sellTickets { while (1) { @synchronized (self) { if ( > 0) { [NSThread sleepForTimeInterval:0.2]; int count = ; = count - 1; NSLog(@"The remaining votes %d",); NSLog(@"Current thread=%@", [NSThread currentThread]); }else { NSLog(@"Tickets sold out"); NSLog(@"Exit thread %@",[NSThread currentThread]); [NSThread exit]; } } } }
Print log
2016-11-04 11:52:25.117 TTTTTTTTTTT[6753:74162] The remaining votes are 9
2016-11-04 11:52:25.117 TTTTTTTTTTTT[6753:74162] Current thread = <NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:25.393 TTTTTTTTTTT[6753:74163] The remaining votes are 8
2016-11-04 11:52:25.393 TTTTTTTTTTTT[6753:74163] Current thread = <NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:25.661 TTTTTTTTTTT[6753:74164] The remaining votes are 7
2016-11-04 11:52:25.661 TTTTTTTTTTTT[6753:74164] Current thread = <NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:25.932 TTTTTTTTTTTT[6753:74162] The remaining votes are 6
2016-11-04 11:52:25.933 TTTTTTTTTTTT[6753:74162] Current thread =<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.164 TTTTTTTTTTT[6753:74163] The remaining votes are 5
2016-11-04 11:52:26.165 TTTTTTTTTTTT[6753:74163] Current thread = <NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:26.438 TTTTTTTTTTTT[6753:74164] The remaining votes are 4
2016-11-04 11:52:26.439 TTTTTTTTTTTT[6753:74164] Current thread = <NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:26.704 TTTTTTTTTTTT[6753:74162] The remaining votes are 3
2016-11-04 11:52:26.705 TTTTTTTTTTTT[6753:74162] Current thread =<NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.975 TTTTTTTTTTTT[6753:74163] The remaining votes are 2
2016-11-04 11:52:26.976 TTTTTTTTTTTT[6753:74163] Current thread = <NSThread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:27.232 TTTTTTTTTTTT[6753:74164] The remaining votes are 1
2016-11-04 11:52:27.233 TTTTTTTTTTTT[6753:74164] Current thread = <NSThread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:27.505 TTTTTTTTTTTT[6753:74162] The remaining votes are 0
2016-11-04 11:52:27.505 TTTTTTTTTTTT[6753:74162] Current thread = <NSThread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:27.505 TTTTTTTTTTTT[6753:74163] Tickets sold out
2016-11-04 11:52:27.506 TTTTTTTTTTTT[6753:74163] Exit thread <NSThread: x608000074540>{number = 4, name = thread2}
We usually use @synchronized to lock threads. What is its use:
(1) Block the thread. The remaining tasks in the thread can only be executed when the code in @synchronized is executed. It is almost the same as the synchronization of the queue.
(2) Before executing the code in @synchronized, the thread must first check whether other threads execute the code in it. If not, continue to execute.
Let’s look at the last item in the print log, which shows that only the thread “thread3” exited, while the other threads did not exit.
In my previous article, the thread will automatically exit after the task is executed. But this is a while loop! If the thread is not exited, the thread will continue to execute.
- (void)sellTickets { while (1) { @synchronized (self) { if ( > 0) { [NSThread sleepForTimeInterval:0.2]; int count = ; = count - 1; NSLog(@"The remaining votes %d",); NSLog(@"Current thread=%@", [NSThread currentThread]); }else { NSLog(@"Tickets sold out"); NSLog(@"Exit thread %@",[NSThread currentThread]); // Don't let threads exit //[NSThread exit]; } } } }
Printed log
2016-11-04 12:01:53.309 TTTTTTTTTTTT[7110:78974] Current thread = <NSThread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.556 TTTTTTTTTTT[7110:78973] The remaining votes are 0
2016-11-04 12:01:53.556 TTTTTTTTTTTT[7110:78973] Current thread = <NSThread: x600000076fc0>{number = 3, name = thread1}
2016-11-04 12:01:53.556 TTTTTTTTTTT[7110:78975] Tickets sold out
2016-11-04 12:01:53.557 TTTTTTTTTTTT[7110:78975] Exit thread <NSThread: x600000077240>{number = 5, name = thread3}
2016-11-04 12:01:53.558 TTTTTTTTTTTT[7110:78974] Tickets sold out
2016-11-04 12:01:53.559 TTTTTTTTTTTT[7110:78974] Exit thread <NSThread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.560 TTTTTTTTTTT[7110:78973] Tickets sold out
Then why is only thread thread2 exiting? (Note: The thread exits each time is uncertain) Because when thread thread2 exits, the method in @synchronized has not been executed, thread thread1 and thread thread3 are still waiting for thread2 to be executed, so they can be executed. But thread thread2 is dead and it is impossible to execute again. This causes thread thread1 and thread thread3 to be in memory all the time and are not exited, causing unnecessary CPU overhead, so it is best not to exit the thread in @synchronized.
- (void)sellTickets { while (1) { @synchronized (self) { if ( > 0) { [NSThread sleepForTimeInterval:0.2]; int count = ; = count - 1; NSLog(@"The remaining votes %d",); NSLog(@"Current thread=%@", [NSThread currentThread]); } } if ( == 0) { NSLog(@"Tickets sold out"); NSLog(@"Exit thread %@",[NSThread currentThread]); [NSThread exit]; } } }
2016-11-04 12:06:51.795 TTTTTTTTTTT[7295:81206] Tickets sold out
2016-11-04 12:06:51.795 TTTTTTTTTTTT[7295:81207] Tickets sold out
2016-11-04 12:06:51.795 TTTTTTTTTTT[7295:81208] Tickets sold out
2016-11-04 12:06:51.796 TTTTTTTTTTT[7295:81206] Exit thread <NSThread: x60000026a3c0>{number = 3, name = thread1}
2016-11-04 12:06:51.796 TTTTTTTTTTT[7295:81207] Exit thread <NSThread: x60000026a380>{number = 4, name = thread2}
2016-11-04 12:06:51.796 TTTTTTTTTTT[7295:81208] Exit thread <NSThread: x60000026a740>{number = 5, name = thread3}
These are some things to note when adding NSThread locks and locks. If you feel it is useful to you, remember to follow me and I hope everyone will support me more.