将项目添加到NSMutableArray之后“重新启动”循环
问题描述:
我有以下代码来模拟我实验室中的过程流程。由此计时器将新项目添加到NSMutablearray。我如何获得while [数组数]!= 0循环来识别新增加的内容?将项目添加到NSMutableArray之后“重新启动”循环
还是有更好的方法吗?
我正在使用GCD块来模拟并行进程。 HEFTBooker对象只需要一个样本编号,然后在“标记”本身可以自由处理另一个样本之前等待定义的时间段。
-(void)runBookingIn:(id)sender {
HEFTLaboratoryUnit *__labUnit = [[HEFTLaboratoryUnit alloc]init];
self.labUnit = __labUnit;
NSMutableArray *sampleNumbers = [[NSMutableArray alloc]init];
// Speed factor speeds up time for modelling i.e. 10 = 10 x normal time.
NSInteger speedFactor = 10;
// Sample arrival rate into laboratory per hour
NSInteger sampleArrivalRate = 50;
__weak id weakSelf = self;
self.timer = [RNTimer repeatingTimerWithTimeInterval:sampleArrivalRate/speedFactor block:^{
[weakSelf addNewSamplesToQueue];
}];
for (int i = 1; i <= 10; i++) {
[sampleNumbers addObject:[NSNumber numberWithInteger:i]];
}
self.labUnit.samplesToBeBookedIn = sampleNumbers;
NSMutableArray *bookerIns = [[NSMutableArray alloc]init];
HEFTBookerIn *webster = [[HEFTBookerIn alloc]init];
webster.bookingInRate = 60;
[email protected]"Webster";
[bookerIns addObject:webster];
HEFTBookerIn *duffy = [[HEFTBookerIn alloc]init];
duffy.bookingInRate = 30;
[email protected]"Duffy";
[bookerIns addObject:duffy];
HEFTBookerIn *marrington = [[HEFTBookerIn alloc]init];
marrington.bookingInRate = 40;
[email protected]"Marrington";
[bookerIns addObject:marrington];
HEFTBookerIn *chatha = [[HEFTBookerIn alloc]init];
chatha.bookingInRate = 10;
[email protected]"Kam";
[bookerIns addObject:chatha];
int i = 0;
long countSamples;
countSamples = [self.labUnit.samplesToBeBookedIn count];
// loop sample numbers
dispatch_queue_t queue;
queue = dispatch_queue_create("com.HEFT.BookerQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t arrivalsQueue;
arrivalsQueue = dispatch_queue_create("com.HEFT.ArrivalsQueue", DISPATCH_QUEUE_SERIAL);
while ([self.labUnit.samplesToBeBookedIn count] != 0)
{
NSInteger sampleNo = [[self.labUnit.samplesToBeBookedIn objectAtIndex:i] integerValue];
long count = [bookerIns count];
int counter = 0;
//loop over booker ins
int j;
for (j=0; j <count ; j++) {
HEFTBookerIn *booker = [bookerIns objectAtIndex:counter];
if (booker.free == YES){
dispatch_sync(queue, ^{
[booker bookingIn:sampleNo :speedFactor];
[self.labUnit.samplesToBeBookedIn removeObjectAtIndex:i];
});
break;
}
else{
counter ++;
}
}
}
}
-(void) addNewSamplesToQueue{
NSLog(@"More Samples Arriving");
for (int i = 1; i <= 10; i++) {
[self.labUnit.samplesToBeBookedIn addObject:[NSNumber numberWithInteger:i]];
}
}
答
这似乎工作,并使添加锁的可变数组添加线程安全。
-(void)runBookingIn:(id)sender {
HEFTLaboratoryUnit *_labUnit = [[HEFTLaboratoryUnit alloc]init];
self.labUnit = _labUnit;
labUnit.booking_in_lock = [[NSLock alloc] init];
labUnit.samplesToBeBookedIn = [[NSMutableArray alloc]init];
// Speed factor speeds up time for modelling i.e. 10 = 10 x normal time.
NSInteger speedFactor = 10;
// Sample arrival rate into laboratory per hour
NSInteger sampleArrivalRate = 50;
__weak id weakSelf = self;
// Add initial samples
[self addNewSamplesToQueue:10];
// Set timer going for further samples
self.timer = [RNTimer repeatingTimerWithTimeInterval:sampleArrivalRate/speedFactor block:^{
[weakSelf addNewSamplesToQueue:10];
}];
// Init booker in Units
NSMutableArray *bookerIns = [[NSMutableArray alloc]init];
HEFTBookerIn *webster = [[HEFTBookerIn alloc]init];
webster.bookingInRate = 60;
[email protected]"Webster";
[bookerIns addObject:webster];
HEFTBookerIn *duffy = [[HEFTBookerIn alloc]init];
duffy.bookingInRate = 30;
[email protected]"Duffy";
[bookerIns addObject:duffy];
HEFTBookerIn *marrington = [[HEFTBookerIn alloc]init];
marrington.bookingInRate = 40;
[email protected]"Marrington";
[bookerIns addObject:marrington];
HEFTBookerIn *chatha = [[HEFTBookerIn alloc]init];
chatha.bookingInRate = 10;
[email protected]"Kam";
[bookerIns addObject:chatha];
// create arrivals and processing loops
dispatch_queue_t processingQueue;
processingQueue = dispatch_queue_create("com.HEFT.BookerQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t arrivalsQueue;
arrivalsQueue = dispatch_queue_create("com.HEFT.ArrivalsQueue", DISPATCH_QUEUE_SERIAL);
// Set timer going for further samples
self.timer2 = [RNTimer repeatingTimerWithTimeInterval:sampleArrivalRate/speedFactor block:^{
[self bookingLoop:arrivalsQueue processingQ:processingQueue bookerInUnits:bookerIns speed:speedFactor];
}];
}
- (void)bookingLoop:(dispatch_queue_t)arrivalsQueue processingQ:(dispatch_queue_t)processingQueue bookerInUnits: (NSMutableArray *)bookerIns speed:(NSInteger)speed{
int i = 0;
long countSamples;
countSamples = [labUnit.samplesToBeBookedIn count];
dispatch_sync(arrivalsQueue, ^{
while ([self.labUnit.samplesToBeBookedIn count] != 0)
{
NSInteger sampleNo = [[self.labUnit.samplesToBeBookedIn objectAtIndex:i] integerValue];
long count = [bookerIns count];
int counter = 0;
//loop over booker ins
int j;
for (j=0; j <count ; j++) {
HEFTBookerIn *booker = [bookerIns objectAtIndex:counter];
if (booker.free == YES){
dispatch_sync(processingQueue, ^{
[booker bookingIn:sampleNo :speed];
[self.labUnit.samplesToBeBookedIn removeObjectAtIndex:i];
});
break;
}
else{
counter ++;
}
}
}
});
}
-(void) addNewSamplesToQueue:(NSInteger) noSamplesToAdd{
NSLog(@"More Samples Arriving %ld", noSamplesToAdd);
[self.labUnit.booking_in_lock lock];
for (int i = 1; i <= 10; i++) {
[labUnit.samplesToBeBookedIn addObject:[NSNumber numberWithInteger:i]];
}
[self.labUnit.booking_in_lock unlock];
}
注意[NSMutableArray的不是线程安全的(http://developer.apple.com/library/mac /#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html) – Sebastian 2013-03-21 22:27:40
并且双下划线保留为compil呃关键字。 – CodaFi 2013-03-22 00:11:20
谢谢,所以我想我必须想出一个不同的方法 – 2013-03-22 12:04:23