IOS多线程-NSthread实现生产者与消费者模式
来源:互联网 发布:浙大网新 人工智能龙头 编辑:程序博客网 时间:2024/05/20 00:14
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_array = [NSMutableArrayarray];
_condition = [[NSConditionalloc] init];
_bIsStop =false;
[selfcreateProductThread];
[selfcreateCustomThread];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) createProductThread{
_productThread = [[NSThreadalloc] initWithTarget:selfselector:@selector(product)object:nil];
[_productThreadstart];
}
-(void) createCustomThread{
_customerThread = [[NSThreadalloc] initWithTarget:selfselector:@selector(customer)object:nil];
[_customerThreadstart];
}
-(void) product{
while(!_bIsStop){
@try{
[_conditionlock]; //获取锁
while(_array.count >=10){
NSLog(@"仓库中满,生产者等待生产");
[_conditionwait];
}
//[NSThread sleepForTimeInterval:0.2];
[_arrayaddObject:@"A"]; //生产者生产东西
NSLog(@"生产了一个产品,库房总数是%ld",_array.count);
[_conditionsignal]; //唤醒在此NSCondition对象上等待的单个线程(通知消费者进行消费)
}@catch(NSException *exception){
NSLog(@"生产程序出现异常 %@",exception);
}@finally{
[_conditionunlock]; //释放锁
}
}
}
-(void)customer{
while(!_bIsStop){
@try{
[_conditionlock];
while(_array.count <=0){
NSLog(@"没有东西可供消费,消费者等待");
[_conditionwait];
}
NSString* obj = [_arrayobjectAtIndex:0]; //取第一个元素;
NSLog(@"消费者取的消费对象是: %@",obj);
[_conditionsignal];
}@catch(NSException* exception){
NSLog(@"消费程序出现异常 %@",exception);
}@finally{
[_conditionunlock]; //释放锁
}
}
}
- (IBAction)stop:(id)sender {
_bIsStop =true;
[_productThreadcancel];
[_customerThreadcancel];
}
@end
- IOS多线程-NSthread实现生产者与消费者模式
- 多线程实现生产者与消费者模式
- IOS多线程使用GCD与信号量实现生产者与消费者模式
- java多线程--ReentrantLock实现生产者与消费者模式
- java多线程实现生产者消费者模式
- Java多线程实现消费者/生产者模式
- Java多线程实现生产者消费者模式
- java 多线程实现生产者消费者模式
- Java多线程实现简单生产者消费者模式
- 用Python多线程实现生产者消费者模式
- 多线程之生产者与消费者模式
- 多线程生产者消费者模式
- 生产者消费者模式 多线程
- 多线程-生产者消费者模式
- Boost多线程实现生产者与消费者问题
- java多线程实现生产者与消费者
- Python中的生产者与消费者 实现多线程
- 生产者-消费者多线程实现
- MXNet如何用mxnet.image.ImageIter直接导入图像
- 原生JavaScript实现斑马线表格
- Qt编程遇到的坑之undefined reference to
- repo的一些常用命令
- 数据压缩原理实验6_实验报告
- IOS多线程-NSthread实现生产者与消费者模式
- What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?
- unity问题与开发技巧汇总(转载专用)
- C语言之数组
- JZOJ 1492. 烤饼干
- Dragonboard410c Android源代码编译:ubuntu16.04 安装使用不同版本JDK
- Windows平台EasyDarwin流媒体服务器部署及使用
- 当坚持不下去的时候
- 利用opencv将raw转换为rgb