美文网首页
GCD和NSOperation实现多线程调用

GCD和NSOperation实现多线程调用

作者: 倪大头 | 来源:发表于2018-03-31 02:29 被阅读6次

GCD:

dispatch_queue_t queue1 = dispatch_queue_create("CONCURRENT", DISPATCH_QUEUE_CONCURRENT);//并发
    // 将任务添加到队列中
    for (NSInteger index = 0; index < 10; index ++) {
        dispatch_async(queue1, ^{//异步
            NSLog(@"任务%ld 线程%@",index,[NSThread currentThread]);
        });
    }

打印结果:

2018-03-31 01:47:32.286672+0800 PodsTest[1301:124255] 任务7 线程<NSThread: 0x604000465400>{number = 10, name = (null)}
2018-03-31 01:47:32.286672+0800 PodsTest[1301:124217] 任务4 线程<NSThread: 0x60000007e040>{number = 7, name = (null)}
2018-03-31 01:47:32.286668+0800 PodsTest[1301:124227] 任务3 线程<NSThread: 0x6040004654c0>{number = 6, name = (null)}
2018-03-31 01:47:32.286668+0800 PodsTest[1301:124226] 任务0 线程<NSThread: 0x60c000265440>{number = 3, name = (null)}
2018-03-31 01:47:32.286686+0800 PodsTest[1301:124230] 任务1 线程<NSThread: 0x6040004653c0>{number = 4, name = (null)}
2018-03-31 01:47:32.286688+0800 PodsTest[1301:124256] 任务8 线程<NSThread: 0x60000007e140>{number = 11, name = (null)}
2018-03-31 01:47:32.286699+0800 PodsTest[1301:124254] 任务6 线程<NSThread: 0x60c000265a00>{number = 9, name = (null)}
2018-03-31 01:47:32.286709+0800 PodsTest[1301:124219] 任务2 线程<NSThread: 0x60c000265980>{number = 5, name = (null)}
2018-03-31 01:47:32.286710+0800 PodsTest[1301:124253] 任务5 线程<NSThread: 0x60000007e000>{number = 8, name = (null)}
2018-03-31 01:47:32.286728+0800 PodsTest[1301:124257] 任务9 线程<NSThread: 0x60800007b780>{number = 12, name = (null)}

NSOperation:
第一种基本用法:

NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocaAction) object:nil];
[invocationOperation start];
- (void)invocaAction {
    NSLog(@"invocation %@",[NSThread currentThread]);
}

打印结果:

2018-03-31 02:02:59.408650+0800 PodsTest[1517:148998] invocation <NSThread: 0x60c000076200>{number = 1, name = main}

第二种基本用法:

NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"任务1 线程%@",[NSThread currentThread]);
}];
[blockOperation addExecutionBlock:^{
    NSLog(@"任务2 线程%@",[NSThread currentThread]);
}];
[blockOperation addExecutionBlock:^{
    NSLog(@"任务3 线程%@",[NSThread currentThread]);
}];
[blockOperation start];

打印结果:

2018-03-31 01:59:43.584362+0800 PodsTest[1469:143515] 任务1 线程<NSThread: 0x604000064c40>{number = 1, name = main}
2018-03-31 01:59:43.584362+0800 PodsTest[1469:143555] 任务3 线程<NSThread: 0x60000006acc0>{number = 4, name = (null)}
2018-03-31 01:59:43.584364+0800 PodsTest[1469:143563] 任务2 线程<NSThread: 0x6080002785c0>{number = 3, name = (null)}

这种方法会优先把任务放到主线程中执行,而且有最大并发数,具体的最大并发数和运行环境有关,如果任务数量大于这个数,那么剩下的任务就会等待某个线程空闲下来之后再被分配到该线程,且依然是优先分配到主线程,同一个线程的block为同步执行
最简便用法:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[operationQueue addOperationWithBlock:^{
    NSLog(@"任务1 线程%@",[NSThread currentThread]);
}];
[operationQueue addOperationWithBlock:^{
    NSLog(@"任务2 线程%@",[NSThread currentThread]);
}];
[operationQueue addOperationWithBlock:^{
    NSLog(@"任务3 线程%@",[NSThread currentThread]);
}];
[operationQueue addOperationWithBlock:^{
    NSLog(@"任务4 线程%@",[NSThread currentThread]);
}];

打印结果:

2018-03-31 02:06:02.480051+0800 PodsTest[1561:154419] 任务2 线程<NSThread: 0x604000078340>{number = 6, name = (null)}
2018-03-31 02:06:02.480057+0800 PodsTest[1561:154413] 任务3 线程<NSThread: 0x60c000260800>{number = 5, name = (null)}
2018-03-31 02:06:02.480069+0800 PodsTest[1561:154420] 任务1 线程<NSThread: 0x60c0002608c0>{number = 3, name = (null)}
2018-03-31 02:06:02.480069+0800 PodsTest[1561:154409] 任务4 线程<NSThread: 0x604000078280>{number = 4, name = (null)}

这种方法最简便,而且可以添加依赖关系

[invo2 addDependency:invo1];

这样invo2一定在invo1执行完以后才会执行,添加依赖的代码一定要在addOperation之前,且不要建立循环依赖,会造成死锁
NSOperation提供cancel方法,也可以用NSOperationQueue的cancelAllOperations取消全部线程,设置NSOperationQueue的suspended属性可以暂停和继续,maxConcurrentOperationCount属性设置最大并发数

GCD是苹果公司为多核运算提供的一套纯C语言解决方案,拥有延迟操作(dispatch_after(时间,队列,任务)),一次性执行(只执行一次,dispatch_once)与调度组(group)等NSOperation不具备的功能

NSOperation是OC中基于GCD的面向对象的封装,对GCD进行了优化,使用比GCD更简单,提供了一些GCD不好实现的功能,且不用关心线程的生命周期,其设置最大并发数,队列的暂停 /取消,指定操作间的依赖关系是GCD不好实现的

相关文章

网友评论

      本文标题:GCD和NSOperation实现多线程调用

      本文链接:https://www.haomeiwen.com/subject/kbwwcftx.html