これが可能かどうかさえわかりませんが、多くの人が多くのサンプルを処理するという実際のプロセスをモデル化したいと思います。
100 個のサンプルがあり、これらのサンプルを処理するために 5 人が利用可能であり、それぞれが 1 時間あたり 20 の速度で処理できるとします。
サンプルを処理オブジェクトに割り当てるプロセッサ クラスの次のコードがあります。
最初の 5 つは問題なく割り当てられますが、タイマーが経過した後、プロセッサが「空き」に設定されないように見えるという点で、無限ループに入るようです。これが発生している理由、またはより良いアプローチに関する提案。目的は、さまざまな速度と数のプロセッサを使用してプロセスをモデル化できるようにすることです。
-(id)init {
if(![super init])
return nil;
self.free = YES;
self.bookingInRate = 20;
return self;
}
-(void)bookingIn:(NSInteger *)sampleNumber {
self.free = NO;
NSNumber *timeToBookIn = [NSNumber numberWithDouble:((60/self.bookingInRate) * 60)/1000];
NSLog(@"Booking in sample number: %ld", sampleNumber);
NSTimeInterval interval = 1;
NSDate *d = [NSDate dateWithTimeIntervalSinceNow: interval];
NSTimer *t = [[NSTimer alloc] initWithFireDate: d
interval: interval
target: self
selector:@selector(onTick:sampleNumber:)
userInfo:nil repeats:NO];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:t forMode: NSDefaultRunLoopMode];
}
-(void)onTick:(NSTimer *)timer sampleNumber:(NSInteger *)sampleNumber {
NSLog(@"Booked in sample no: %ld", sampleNumber);
self.free = YES;
}
次に、次を使用してモデリング ループを実行します。
NSMutableArray *sampleNumbers = [[NSMutableArray alloc]init];
for (int i = 1; i <= 10; i++) {
[sampleNumbers addObject:[NSNumber numberWithInteger:i]];
}
NSMutableArray *bookerIns = [[NSMutableArray alloc]init];
for (int i=1; i<=5; i++) {
[bookerIns addObject:[[HEFTBookerIn alloc]init]];
}
int i = 0;
long countSamples;
countSamples = [sampleNumbers count];
// loop sample numbers
while ([sampleNumbers count] != 0 )
{
NSInteger sampleNo = [[sampleNumbers 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_async(queue, ^{
[booker bookingIn:sampleNo];
});
NSLog(@"Booking in : %ld", sampleNo);
[booker bookingIn:sampleNo];
[sampleNumbers removeObjectAtIndex:i];
//i ++;
break;
} else {
counter ++;
}
}
}