NSThread 售票员售票问题
2015-06-23 14:05
309 查看
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.tickets = 20; NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil]; threadA.name = @"售票员 A"; [threadA start]; NSThread *threadB = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil]; threadB.name = @"售票员 B"; [threadB start]; } - (void)saleTickets { while (YES) { // 模拟休眠 [NSThread sleepForTimeInterval:1.0f]; //枷锁 防止资源抢夺错误 @synchronized(self) { if (self.tickets > 0) { self.tickets--; NSLog(@"剩余票数 %@ %d", [NSThread currentThread], self.tickets); } else { break; } } } }
相关文章推荐
- Linux ALSA声卡驱动之六:ASoC架构中的Machine
- 正则匹配URL地址
- 什么是blob,mysql blob大小配置介绍
- 黑马程序员JAVA笔记9--反射
- 修改 QQ 聊天记录保存路径 ,并禁止随意更改
- 在zabbix中添加监控主机及Items
- centOS安装telnet服务和xinetd服务和iptables和linux运行级别
- javascript 数据类型
- 【BZOJ2823】【AHOI2012】信号塔 最小圆覆盖 计算几何
- ORACLE常用SQL命令
- JS小知识点三(for gis):openAtStart
- JPA常用注解
- Linux ALSA声卡驱动之五:移动设备中的ALSA(ASoC)
- C++函数参数中的省略号用法分析
- 【剑指offer 面试题21】包含min函数的栈
- Android 如何让EditText不自动获取焦点
- 设计模式----状态模式 实例
- VS2008在XP系统托盘气泡提示不成功解决办法
- 【iOS知识学习】_视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
- 如何查看php版本