#import "ViewController.h"@interface ViewController ()/// 销售员01@property (strong, nonatomic) NSThread *thread01;/// 销售员02@property (strong, nonatomic) NSThread *thread02;/// 销售员03@property (strong, nonatomic) NSThread *thread03;/// 票的总数@property (assign, nonatomic) NSInteger ticketCount;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.ticketCount = 100; self.thread01 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil]; self.thread01.name = @"销售员01"; self.thread02 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil]; self.thread02.name = @"销售员02"; self.thread03 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil]; self.thread03.name = @"销售员03";}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event { [self.thread01 start]; [self.thread02 start]; [self.thread03 start];}- (void)saleTicket{ while (1) { @synchronized(self) { // 取出总数 NSInteger count = self.ticketCount; if (count > 0) { self.ticketCount = count -1; NSLog(@"%@卖了一张,还剩下%zd张",[NSThread currentThread],self.ticketCount); } else { NSLog(@"票已经卖完了"); break; } } }}@end