使用NSCondition实现不同线程等待唤醒

2017-06-04  本文已影响203人  Heikki_

NSCondition : 一个线程可能需要等待其它线程返回结果.
本例中,使用 condition 对"生产","购买"两个线程加锁,实现
->生产一个商品
->发生一次购买
->再生产一个商品
循环.
[condition wait]之后当前线程会被阻塞直到 [condition signal]
1.生产

@interface Producer : NSObject
@property (nonatomic, assign) BOOL shouldProduce;

@property (nonatomic, strong) NSString *itemName;

@property (nonatomic, strong) NSCondition *condition;

@property (nonatomic, strong) NSMutableArray *collector;

- (instancetype)initWithConditon:(NSCondition *)condition collector:(NSMutableArray *)collector;
- (void)produce;
@end

@implementation Producer

- (instancetype)initWithConditon:(NSCondition *)condition collector:(NSMutableArray *)collector{
    
    self = [super init];
    if (self) {
        self.condition = condition;
        self.collector = collector;
        self.shouldProduce = NO;
        self.itemName = nil;
    }
           return self;
}

-(void)produce{
    self.shouldProduce = YES;
    while (self.shouldProduce) {
        [self.condition lock];
        if (self.collector.count > 0 ) {
            [self.condition wait];
        }
        [self.collector addObject:@"iPhone"];
        NSLog(@"生产:iPhone");
        [self.condition signal];
        [self.condition unlock];
    }
}

2.消费者

#import <Foundation/Foundation.h>

@interface Consumer : NSObject
@property (nonatomic, assign) BOOL shouldConsumer;

@property (nonatomic, strong) NSString *itemName;

@property (nonatomic, strong) NSCondition *condition;

@property (nonatomic, strong) NSMutableArray *collector;
- (instancetype)initWithConditon:(NSCondition *)condition collector:(NSMutableArray *)collector;
- (void)consumer;
@end

#import "Consumer.h"

@implementation Consumer
- (instancetype)initWithConditon:(NSCondition  *)condition collector:(NSMutableArray *)collector{
    
    self = [super init];
    if (self) {
        self.condition = condition;
        self.collector = collector;
        self.shouldConsumer = NO;
        self.itemName = nil;
    }
    return self;
}

-(void)consumer{
    self.shouldConsumer = YES;
    while (self.shouldConsumer) {
        [self.condition lock];
        if (self.collector.count == 0 ) {
            [self.condition wait];
        }
        
        NSString *item = [self.collector objectAtIndex:0];
        NSLog(@"买入:%@",item);
        [self.collector removeObjectAtIndex:0];
        [self.condition signal];
        [self.condition unlock];
    }
}
@end

3.开始抢购

    NSMutableArray *pipeline = [NSMutableArray array];
    NSCondition *conditon = [NSCondition new];
    
    Producer *p = [[Producer alloc]initWithConditon:conditon collector:pipeline];
    Consumer *c = [[Consumer alloc]initWithConditon:conditon collector:pipeline];
    [[[NSThread alloc] initWithTarget:p selector:@selector(produce) object:p] start];
    [[[NSThread alloc] initWithTarget:c selector:@selector(consumer) object:c] start];

4.结束生产,购买

  p.shouldProduce = NO;
  c.shouldConsumer = NO;

5.结果

2017-06-04 20:55:18.051 Condition[63117:2614482] 生产:iPhone
2017-06-04 20:55:18.051 Condition[63117:2614483] 买入:iPhone
2017-06-04 20:55:18.051 Condition[63117:2614482] 生产:iPhone
2017-06-04 20:55:18.052 Condition[63117:2614483] 买入:iPhone
2017-06-04 20:55:18.052 Condition[63117:2614482] 生产:iPhone
2017-06-04 20:55:18.052 Condition[63117:2614483] 买入:iPhone
2017-06-04 20:55:18.053 Condition[63117:2614482] 生产:iPhone
2017-06-04 20:55:18.053 Condition[63117:2614483] 买入:iPhone
2017-06-04 20:55:18.054 Condition[63117:2614482] 生产:iPhone
...
无限循环

form:<高性能iOS应用开发>
(原书中有些错误,此处已修改.不知道是原版还是翻译的锅,这本书里错误巨多)

上一篇 下一篇

猜你喜欢

热点阅读