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
原创粉丝点击