下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
@interface VIEwController ()@property (nonatomic,strong) NSMutableArray * array;@property (nonatomic,strong) NSCondition * condition;@property (nonatomic,strong) NSTimer * timer;@end@implementation VIEwController//初始化- (NSMutableArray *)array{ if (!_array) { _array = [NSMutableArray array]; } return _array;}- (NSCondition *)condition{ if (!_condition) { _condition = [[NSCondition alloc] init]; } return _condition;}//加载- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; //购买 for (int i = 0; i<3; i++) { [self performSelectorInBackground:@selector(_consumer) withObject:nil]; } //生产 self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(_productor) userInfo:nil repeats:YES]; }- (voID)_productor{ [self.condition lock]; if (self.array.count != 0) { NSLog(@"结束"); [self.timer invalIDate]; self.timer = nil; } else { [self.array addobject:@"aaa"]; NSLog(@"生产完成!"); [self.condition signal]; } [self.condition unlock]; }- (voID)_consumer{ [self.condition lock]; if (self.array.count <= 0) { NSLog(@"正在等待!"); [self.condition wait]; } [self.array removeLastObject]; NSLog(@"购买成功!"); [self.condition unlock];}@end
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的NSCondition模拟买票的过程的简单实现全部内容,希望文章能够帮你解决NSCondition模拟买票的过程的简单实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)