iOS进阶

iOS多任务并发设计

2017-08-29  本文已影响74人  起个名字想破头

问题假如现在有1w个任务需要执行,并且在全部执行完成之后进行一个提示,该怎么做?

思路1:最直接的会想到dispatch_group,本身就是为一组任务设计的

#define OPERATION_COUNT         10000
#define OPERATION_SLEEP_TIME    0.01f

- (void)myOperation:(NSInteger)index {
    [NSThread sleepForTimeInterval:OPERATION_SLEEP_TIME];    
}

- (void)runMyOperationsWithGCD {
    dispatch_group_t group = dispatch_group_create();
    for (int i = 0; i < OPERATION_COUNT; i++) {
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            [self myOperation:i];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"operations are finished");
    });
}

思考1:这段代码看的应该非常清爽,但是会创建过多的线程,也给系统带来了巨大的负担,是需要优化的。


问题假设在原始问题的基础上约束最多10个并发线程,又有什么好的办法?

思路2:采用iOS的NSOperationQueue,这个东西简直是按照这种需求设计的,非常好用

#define MAX_CONCURRENT_COUNT 10
#define OPERATION_COUNT     10000
#define OPERATION_SLEEP_TIME    0.1f

- (void)myOperation:(NSInteger)index {
    NSLog(@"enter myOperation:%ld, thread:%@", (long)index, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:OPERATION_SLEEP_TIME];    
}

- (void)runMyOPerationsWithNSOperation {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = MAX_CONCURRENT_COUNT;//设置最大并发数
    for (int i = 0; i < OPERATION_COUNT; i++) {
        [queue addOperationWithBlock:^{
            [self myOperation:i];
        }];
    }
        
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [queue waitUntilAllOperationsAreFinished];//等待所有任务完成
        NSLog(@"operations are finished");
    });
}

思考2:个人觉得,这是最方便的方法,简单明了,没有过多的线程和冗余的代码。


问题如果不使用NSOperation,又有什么好的办法吗?

思路3:通过信号量控制并发,并结合思路1的dispatch_group实现组任务完成后的通知

#define MAX_CONCURRENT_COUNT 10
#define OPERATION_COUNT     10000
#define OPERATION_SLEEP_TIME    0.1f

- (void)myOperation:(NSInteger)index {
    NSLog(@"enter myOperation:%ld, thread:%@", (long)index, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:OPERATION_SLEEP_TIME];
}

- (void)runMyOperationsWithSemaphore {
    //创建信号量,控制最大并发数为10
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(MAX_CONCURRENT_COUNT);
    dispatch_group_t group = dispatch_group_create();
    for (int i = 0; i < OPERATION_COUNT; i++) {
        //wait一次,semaphore减一,当semaphore小于0时会一直wait
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            [self myOperation:i];
            //任务完成后,触发semaphore,以允许新的任务进入
            dispatch_semaphore_signal(semaphore);
        });
    }

    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"operations are finished");
    });
}

思考3:这种方式其实是在思路1的基础上增加了信号量的并发限制,相当于一种简单的改进。

最后,如果采用最传统的NSThread,跟信号量配合,可以实现多任务并发,但如何判断所有任务都已执行完毕,只想到了通过计数的方式,并没有想到更好的办法。如果大家有更好的办法,欢迎留言,私信!

最最后,附上demo,地址

上一篇 下一篇

猜你喜欢

热点阅读