NSCondition : 一個(gè)線程可能需要等待其它線程返回結(jié)果.
本例中,使用 condition
對(duì)"生產(chǎn)","購(gòu)買(mǎi)"兩個(gè)線程加鎖,實(shí)現(xiàn)
->生產(chǎn)一個(gè)商品
->發(fā)生一次購(gòu)買(mǎi)
->再生產(chǎn)一個(gè)商品
循環(huán).
[condition wait]
之后當(dāng)前線程會(huì)被阻塞直到 [condition signal]
1.生產(chǎn)
@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(@"生產(chǎn):iPhone");
[self.condition signal];
[self.condition unlock];
}
}
2.消費(fèi)者
#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(@"買(mǎi)入:%@",item);
[self.collector removeObjectAtIndex:0];
[self.condition signal];
[self.condition unlock];
}
}
@end
3.開(kāi)始搶購(gòu)
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.結(jié)束生產(chǎn),購(gòu)買(mǎi)
p.shouldProduce = NO;
c.shouldConsumer = NO;
5.結(jié)果
2017-06-04 20:55:18.051 Condition[63117:2614482] 生產(chǎn):iPhone
2017-06-04 20:55:18.051 Condition[63117:2614483] 買(mǎi)入:iPhone
2017-06-04 20:55:18.051 Condition[63117:2614482] 生產(chǎn):iPhone
2017-06-04 20:55:18.052 Condition[63117:2614483] 買(mǎi)入:iPhone
2017-06-04 20:55:18.052 Condition[63117:2614482] 生產(chǎn):iPhone
2017-06-04 20:55:18.052 Condition[63117:2614483] 買(mǎi)入:iPhone
2017-06-04 20:55:18.053 Condition[63117:2614482] 生產(chǎn):iPhone
2017-06-04 20:55:18.053 Condition[63117:2614483] 買(mǎi)入:iPhone
2017-06-04 20:55:18.054 Condition[63117:2614482] 生產(chǎn):iPhone
...
無(wú)限循環(huán)
form:<高性能iOS應(yīng)用開(kāi)發(fā)>
(原書(shū)中有些錯(cuò)誤,此處已修改.不知道是原版還是翻譯的鍋,這本書(shū)里錯(cuò)誤巨多)