1、苹果,橘子,葡萄,每种水果在2-5s随机时间内,随机生成8-20的价格,然后对价格进行排序
{
NSMutalbeDictionary *dictionary = [NSMutalbeDictionary dictionary];
void (^reArrange)(void) = ^{
NSMutableArray*array = [NSMutableArray array];
for (NSString *key in self.dictionary.allKeys) {
[array addObject:@{key: self.dictionary[key]}];
}
NSArray *sortArr = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSDictionary *dic1 = obj1;
NSDictionary *dic2 = obj2;
if ([dic1.allValues.firstObject intValue] > [dic2.allValues.firstObject intValue]) {
return NSOrderedDescending;
} else {
return NSOrderedAscending;
}
}];
for (NSDictionary *dic in sortArr) {
printf("%s:%s,", [dic.allKeys.firstObject UTF8String], [[NSString stringWithFormat:@"%@", dic.allValues.firstObject]UTF8String]);
}
printf("\n");
};
dispatch_queue_t asyncqueue = dispatch_queue_create("com.aa.aa.aa", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(asyncqueue, ^{
int time = arc4random() % 6 + 1;
NSTimer *timer = [NSTimer timerWithTimeInterval:time repeats:YES block:^(NSTimer * _Nonnull timer) {
@synchronized (self) {
size_t price = arc4random() % 4 + 8;
[self.dictionary setObject:@(price) forKey:@"apple"];
reArrange();
NSLog(@"---------------------apple---------------------");
}
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
});
dispatch_async(asyncqueue, ^{
int time = arc4random() % 6 + 1;
NSTimer *timer = [NSTimer timerWithTimeInterval:time repeats:YES block:^(NSTimer * _Nonnull timer) {
@synchronized (self) {
size_t price = arc4random() % 4 + 8;
[self.dictionary setObject:@(price) forKey:@"orange"];
reArrange();
NSLog(@"---------------------orange---------------------");
}
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
});
dispatch_async(asyncqueue, ^{
int time = arc4random() % 6 + 1;
NSTimer *timer = [NSTimer timerWithTimeInterval:time repeats:YES block:^(NSTimer * _Nonnull timer) {
@synchronized (self) {
size_t price = arc4random() % 4 + 8;
[self.dictionary setObject:@(price) forKey:@"grape"];
reArrange();
NSLog(@"---------------------grape---------------------");
}
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
});
}






网友评论