1 #import "ViewController.h" 2 3 @interface ViewController () 4 //总票数 5 @property (nonatomic,assign) NSInteger count; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 self.count = 100; 13 14 } 15 - (IBAction)didClickSell:(id)sender { 16 17 //线程锁 18 NSLock *lockObject = [[NSLock alloc] init]; 19 for (int i = 0; i < 100;i++) { 20 //并行队列 21 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 22 [lockObject lock]; 23 NSLog(@"我买到了第%ld张票",(long)self.count); 24 self.count--; 25 [lockObject unlock]; 26 }); 27 } 28 29 }