NSThread 的卖票(加锁) Demo
来源:互联网 发布:js识别微信浏览器 编辑:程序博客网 时间:2024/05/10 04:08
- (void)viewDidLoad { [super viewDidLoad]; _count = 50; //创建锁 _lock = [[NSLock alloc]init]; //主线程 NSLog(@"主线程%@",[NSThread currentThread]); //创建线程 [NSThread detachNewThreadSelector:@selector(sellTickets) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(sellTickets) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(sellTickets) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(sellTickets) toTarget:self withObject:nil];}-(void) sellTickets{/* while (_count>3) { [_lock lock]; //加锁防止资源同时被两个以上线程占用 _count--; NSLog(@"%@--票数:%ld",[NSThread currentThread],_count); [_lock unlock]; }*/ //第二种 while (_count>0) { @synchronized(self){ //加锁防止资源同时被两个以上线程占用 [NSThread sleepForTimeInterval:0.2]; if (_count>0) { _count--; NSLog(@"%@--票数:%ld",[NSThread currentThread],_count); } } } }
0 0
- NSThread 的卖票(加锁) Demo
- 多线程同步卖票demo
- 多线程卖票之-NSThread、NSOperation、GCD
- IOS学习 多线程NSThread 共享变量 卖票
- 卖票
- 卖票
- 卖票
- 卖票
- java简单的卖票程序
- 多线程实现卖票的例子
- iphone的多线程NSThread
- NSThread的使用
- NSThread的使用
- NSThread的使用
- 学习:NSThread的使用
- NSThread的使用
- 88-NSThread的使用
- NSThread的使用
- ios-AutoLayout(自动布局代码控制)简单总结
- 正确的JQuery.ajax写法
- 趣学Python-教孩子学编程--第十二章
- 浅谈Java中的Set、List、Map的区别
- JAVA框架——Maven
- NSThread 的卖票(加锁) Demo
- Local declaration of 'XXX' hides instance variable
- STM32f407驱动hc-sr04超声波测距模块
- jquery动态调整表格宽度
- UI通讯录
- stackoverflow上SegFault对System.out.flush()的解释
- 1033. To Fill or Not to Fill (25)【贪心+模拟】——PAT (Advanced Level) Practise
- C plus plus(2)---using命名空间
- 函数基础