0422 线程同步/同步锁防止脏数据
@interface ViewController ()
{
NSInteger _money;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_money = 100;
NSThread *t1 = [[NSThread alloc]initWithTarget:self selector:@selector(doThing) object:nil];
NSThread *t2 = [[NSThread alloc]initWithTarget:self selector:@selector(doThing) object:nil];
NSThread *t3 = [[NSThread alloc]initWithTarget:self selector:@selector(doThing) object:nil];
NSThread *t4 = [[NSThread alloc]initWithTarget:self selector:@selector(doThing) object:nil];
t1.name = @"我是--t1";
t2.name = @"我是--t2";
t3.name = @"我是--t3";
t4.name = @"我是--t4";
[t1 start];
[t2 start];
[t3 start];
[t4 start];
}
-(void)doThing{
-(void)getMoney:(NSInteger)money{
// 查余额
if(_money >= money){
[NSThread sleepForTimeInterval:2];
_money -= money;
NSLog(@"%@----余额 : %ld",[NSThread currentThread],_money);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。