多次并行执行类的方法

时间:2022-05-19 13:51:53

I have an array- say wordsArray where each object of wordArray is another array- say wordList. With for loop i am picking each object (wordList) and call a method -say wordScore of another class which will return NSDictionary with word and the score. Instead of calling wordScore for each object one by one, Is there a way I can run them parallel? wordScore for each wordList should execute in parallel and at the end of execution all the NSDictionaries of each wordScore should be merged into a single NSDictionary.

我有一个数组 - 比如说WordsArray,其中wordArray的每个对象都是另一个数组 - 比如wordList。使用for循环,我选择每个对象(wordList)并调用另一个类的方法-say wordScore,它将返回带有单词和分数的NSDictionary。而不是逐个调用每个对象的wordScore,有没有办法可以并行运行它们?每个wordList的wordScore应该并行执行,并且在执行结束时,每个wordScore的所有NSDictionaries都应该合并为一个NSDictionary。

1 个解决方案

#1


1  

Let's say I have something silly like this class

假设我有类似这门课的傻事

@interface MyWordClass: NSObject
- (NSDictionary *)wordScore:(NSArray *)wordList;
@end

@implementation MyWordClass
- (NSDictionary *)wordScore:(NSArray *)wordList {
    NSMutableDictionary *scores = [NSMutableDictionary new];
    for (NSString *word in wordList) {
        scores[word] = @(word.hash %100);
    }
    return [scores copy];
}
@end

From here, I'll do a concurrent enumeration of wordsArray. I create a new instance of MyWordClass in each of the concurrent iterations. Finally, I use a @synchronized block to accumulate the results.

从这里开始,我将对wordsArray进行并发枚举。我在每个并发迭代中创建一个MyWordClass的新实例。最后,我使用@synchronized块来累积结果。

NSArray *wordsArray = @[@[@"a", @"b", @"c"], @[@"1", @"2", @"3"]];

NSMutableDictionary *result = [NSMutableDictionary new];
[wordsArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSArray *wordList, NSUInteger idx, BOOL *stop) {
    // The scores are being calculated concurrently
    NSDictionary *scores = [[MyWordClass new] wordScore:wordList];

    @synchronized(result) {
        [result addEntriesFromDictionary:scores];
    }
}];

NSLog(@"%@", result);

Important take aways are 1) do a concurrent enumeration, 2) don't use shared instances in the concurrent block, and 3) use a @synchronized block to gather the result.

重要的一点是1)执行并发枚举,2)不要在并发块中使用共享实例,3)使用@synchronized块来收集结果。

#1


1  

Let's say I have something silly like this class

假设我有类似这门课的傻事

@interface MyWordClass: NSObject
- (NSDictionary *)wordScore:(NSArray *)wordList;
@end

@implementation MyWordClass
- (NSDictionary *)wordScore:(NSArray *)wordList {
    NSMutableDictionary *scores = [NSMutableDictionary new];
    for (NSString *word in wordList) {
        scores[word] = @(word.hash %100);
    }
    return [scores copy];
}
@end

From here, I'll do a concurrent enumeration of wordsArray. I create a new instance of MyWordClass in each of the concurrent iterations. Finally, I use a @synchronized block to accumulate the results.

从这里开始,我将对wordsArray进行并发枚举。我在每个并发迭代中创建一个MyWordClass的新实例。最后,我使用@synchronized块来累积结果。

NSArray *wordsArray = @[@[@"a", @"b", @"c"], @[@"1", @"2", @"3"]];

NSMutableDictionary *result = [NSMutableDictionary new];
[wordsArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSArray *wordList, NSUInteger idx, BOOL *stop) {
    // The scores are being calculated concurrently
    NSDictionary *scores = [[MyWordClass new] wordScore:wordList];

    @synchronized(result) {
        [result addEntriesFromDictionary:scores];
    }
}];

NSLog(@"%@", result);

Important take aways are 1) do a concurrent enumeration, 2) don't use shared instances in the concurrent block, and 3) use a @synchronized block to gather the result.

重要的一点是1)执行并发枚举,2)不要在并发块中使用共享实例,3)使用@synchronized块来收集结果。