IOS-多线程
来源:互联网 发布:c语言&.2f是什么意思 编辑:程序博客网 时间:2024/06/15 22:21
NSThread简单的线程同步
这里写代码片@interface FZViewController ()@property (nonatomic, assign) int leftTicksNum;@property (nonatomic, strong) NSThread *thread1;@property (nonatomic, strong) NSThread *thread2;@property (nonatomic, strong) NSThread *thread3;@end@implementation FZViewController- (void)viewDidLoad{ [super viewDidLoad]; self.leftTicksNum = 10; self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTick) object:nil]; self.thread1.name = @"thread1"; self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTick) object:nil]; self.thread2.name = @"thread2"; self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTick) object:nil]; self.thread3.name = @"thread3";}- (void)saleTick{ NSThread *thread = [NSThread currentThread]; while (1) { @synchronized(self) { if (self.leftTicksNum <= 0) break; [NSThread sleepForTimeInterval:(arc4random() % 10) * 0.1]; NSLog(@"当前还剩:%d %@售出一张,还剩:%d张", self.leftTicksNum, thread.name, self.leftTicksNum - 1); self.leftTicksNum--; } } [NSThread exit];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.thread1 start]; [self.thread2 start]; [self.thread3 start];}@end
0 0
- ios多线程
- iOS多线程
- iOS多线程
- iOS多线程
- 多线程ios
- IOS 多线程
- ios多线程
- ios 多线程
- IOS多线程
- IOS多线程
- iOS多线程
- iOS多线程
- ios 多线程
- iOS多线程
- IOS多线程
- ios多线程
- iOS 多线程
- iOS 多线程
- 习题3-40
- 2015061103 - apache httpclient jar包下载地址
- 认识自己——当断则断
- 教程:一分钟完成SiteMesh Template模板组合
- 斯坦福大学iOS开发公开课总结
- IOS-多线程
- 浅谈jsp、freemarker、velocity区别
- C Primer Plus 第五章习题
- Java Exception异常
- 服务器构架的基础1
- 子线程调用Toast报Can't create handler inside ~~~ 错误
- hiho一下 第四十九周 -题目1 : 欧拉路·一
- Velocity 入门之 HelloWorld
- 暗网非法交易被判终身监禁是否严重,反正他是不服的