IOS 生产者-消费者模型实现
来源:互联网 发布:从零开始写python爬虫 编辑:程序博客网 时间:2024/05/22 05:34
//// ViewController.m// pc_mode//// Created by ise on 16/1/25.// Copyright © 2016年 lzb. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property(assign ,nonatomic)int productNum;@property(strong ,nonatomic)NSCondition *cond;@end@implementation ViewController-(void)producerHandle { while (1) { NSString *currentThreadName = [NSThread currentThread].name;// NSLog(@"%@" ,currentThreadName); //生产者限制生产数 [self.cond lock]; if (self.productNum > 10) { NSLog(@"!!!!生产太多,限制"); [self.cond unlock]; sleep(1); continue; }else { [self.cond unlock]; } [self.cond lock]; NSLog(@"============================"); NSLog(@"%@...Begin - %d" ,currentThreadName ,self.productNum); self.productNum++; [self.cond signal]; NSLog(@"%@...end - %d" ,currentThreadName ,self.productNum); NSLog(@"============================"); [self.cond unlock]; }}-(void)customerHandle { while (1) { NSString *currentThreadName = [NSThread currentThread].name;// NSLog(@"%@" ,currentThreadName); [self.cond lock]; NSLog(@"============================"); NSLog(@"%@...Begin - %d" ,currentThreadName ,self.productNum); while (self.productNum <= 0) { [self.cond wait]; } self.productNum--; NSLog(@"%@...end - %d" ,currentThreadName ,self.productNum); NSLog(@"============================"); [self.cond unlock]; }}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.productNum = 0; self.cond = [[NSCondition alloc]init]; NSThread *producerThread1 = [[NSThread alloc]initWithTarget:self selector:@selector(producerHandle) object:nil]; producerThread1.name = @"生产者1"; NSThread *producerThread2 = [[NSThread alloc]initWithTarget:self selector:@selector(producerHandle) object:nil]; producerThread2.name = @"生产者2"; NSThread *producerThread3 = [[NSThread alloc]initWithTarget:self selector:@selector(producerHandle) object:nil]; producerThread1.name = @"生产者3"; NSThread *producerThread4 = [[NSThread alloc]initWithTarget:self selector:@selector(producerHandle) object:nil]; producerThread2.name = @"生产者4"; NSThread *customerThread1 = [[NSThread alloc]initWithTarget:self selector:@selector(customerHandle) object:nil]; customerThread1.name = @"消费者1"; NSThread *customerThread2 = [[NSThread alloc]initWithTarget:self selector:@selector(customerHandle) object:nil]; customerThread2.name = @"消费者2"; [producerThread1 start]; [producerThread2 start]; [producerThread3 start]; [producerThread4 start]; [customerThread1 start]; [customerThread2 start];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- IOS 生产者-消费者模型实现
- 生产者-消费者模型实现
- 简单实现生产者、消费者模型
- java实现生产者消费者模型
- 生产者消费者模型的实现
- LinkedBlockingQueue 实现生产者消费者模型
- OpenMP实现生产者消费者模型
- Java实现生产者-消费者模型
- Java---实现生产者消费者模型
- TransferQueue实现生产者、消费者模型
- 队列实现生产者消费者模型
- Java实现生产者消费者模型
- pthread-消费者/生产者模型实现
- Linux实现生产者消费者模型
- 用链表实现生产者消费者模型
- 生产者消费者模型的实现
- 简单的IOS生产者-消费者模型
- 生产者消费者模型实现<一>模拟实现
- Java调用Runtime.getRuntime().exec(commandStr)来调用cmd执行指令
- activity启动模式
- c#之交换两个变量的值
- CUDA配置
- 从NSURLConnection到NSURLSession
- IOS 生产者-消费者模型实现
- Spark Streaming:性能调优
- 涅磐重生 http://cobaya.cn/ visualVM集成到Eclipse针对性能监控
- jquery 绘图工具 flot 使用
- 关于SSE指令集
- 商业英语句型13
- 单例模式的例子
- 在mac下通过Terminal修改host文件
- C++中this指针存放在哪里