注意:加锁位置不同产生结果不同

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{ //设置中票数
self.totalCount = ; self.threadA = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
self.threadB = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
self.threadC = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil]; self.threadA.name = @"售票员A";
self.threadB.name = @"售票员B";
self.threadC.name = @"售票员C"; //启动线程
[self.threadA start];
[self.threadB start];
[self.threadC start];
} -(void)saleTicket
{
while () { //锁:必须是全局唯一的
//1.注意枷锁的位置
//2.注意枷锁的前提条件,多线程共享同一块资源
//3.注意加锁是需要代价的,需要耗费性能的
//4.加锁的结果:线程同步 @synchronized(self) {
//线程1
//线程2
//线程3
NSInteger count = self.totalCount;
if (count >) { for (NSInteger i = ; i<; i++) {
} self.totalCount = count - ;
//卖出去一张票
NSLog(@"%@卖出去了一张票,还剩下%zd张票", [NSThread currentThread].name,self.totalCount);
}else
{
NSLog(@"不要回公司上班了");
break;
}
}
} }

IOS多线程加锁-LMLPHP

IOS多线程加锁-LMLPHP

05-11 15:41