GCD NSOperation 拾遗

  1. 并发 vs 并行

    1. 并发: 一段时间内存在多个执行路径,单核cpu,分时执行
    2. 并发: 同一时刻,存在多个执行路径的任务
  2. 同步 vs 异步

    1. 同步,阻塞当前队列的执行,执行完毕之后,进行执行
    2. 异步,只是添加block到队列中去,并不等待执行
  3. 串行 vs 并行

    1. 串行 FIFO的执行顺序
    2. 并行 任务提交按FIFO,任务的执行不是
  4. NSBlockOperation vs NSInvocationOperation

    1. BlockOperation, 并发操作, 可以捕获更多的 参数,同时可以添加 多个block
    2. InvocationOperation ,串行操作,对已有的方法,直接设定会比较合适
  5. NSOperation 是如何执行的?

    1. operation 先check 依赖关系,如果有,先完成依赖操作,如果没有,开始执行
    2. 如果并发设置为1,则FIFO队列顺序执行,有优先级排序的设定
    3. 否则将并行执行,多个操作
    4. operation 执行的影响因素有3个,添加到queue 的顺序,依赖关系,它所在队列的优先级
  6. 主队列能否修改并发数?

    1. 不能,及时你修改了,也是无效的。
  7. GCD 是否一定开辟新线程?

    1. 线程池循环使用
  8. dispatch_once 如何实现的?

  9. 信号量控制并发数 dispatch_semaphore

    // 创建队列组
    dispatch_group_t group = dispatch_group_create();
    // 创建信号量,并且设置值为3
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(3);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (int i = 0; i < 100; i++)
    {
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_group_async(group, queue, ^{
            NSLog(@"%i",i);
            sleep(2);
            // 每次发送信号则semaphore会+1,
            dispatch_semaphore_signal(semaphore);
        });
    }
    
  10. 多线程是为了解决什么问题而引入的概念?

  11. 什么情况下你会使用多线程?对于一个可分治的问题规模为 n 的任务,多线程和单线程两种方式哪个能更快完成任务?

  12. NSOperation 是为了解决什么问题而引入的?

  13. GCD 是为了解决什么问题而引入的?什么时候你会用 NSThread?什么时候你会用 NSOperation?什么时候你会用 GCD?

  14. 队列 vs 线程的区别

    let key = DispatchSpecificKey()
    DispatchQueue.main.setSpecific(key: key, value: "main")
    func log() {
        debugPrint("main thread: \(Thread.isMainThread)")
        let value = DispatchQueue.getSpecific(key: key)
        debugPrint("main queue: \(String(describing: value)) \(value != nil)")
    }
    DispatchQueue.global().sync(execute: log)
    
      // "main thread: true"
        //"main queue: nil false"
        // 结论:队列是对执行item 的管理,线程是真正的执行体
    
  15. GCD NSOperation 比较

    • GCD是纯C语言的API,NSOPerationQueue是基于GCD的OC版本的封装;

    • GCD只支持FIFO的队列,NSOPerationQueue可以很方便的调整执行顺序、设置最大并发数量;

    • NSOPerationQueue可以轻松在Operation间设置依赖关系,而GCD需要写很多的代码才能实现;

    • NSOperationQueue支持KVO,可以检测operation是否正在执行(isExecuted)、是否结束(isFinished)、是否取消(isCanceled);

    • GCD的执行速度比NSOperationQueue快

      适用范围:

      • 任务之间不太相互依赖:GCD
      • 任务之间有依赖或者要监听任务的执行情况:NSOperationQueue
    NSLog(@"0 %@", NSThread.currentThread);

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1 %@", NSThread.currentThread);
    }];
    
    for (int i=2; i<50; i++) {
        [operation addExecutionBlock:^{
            NSLog(@"%d %@", i, NSThread.currentThread);
        }];
    }
    
    NSLog(@"5.0 %@", NSThread.currentThread);

    operation.completionBlock = ^{
        NSLog(@"finished %@", NSThread.currentThread);
    };
    NSLog(@"5.1 %@", NSThread.currentThread);
    [[NSOperationQueue mainQueue] addOperation:operation];
    NSLog(@"6 %@", NSThread.currentThread);

上面的代码总结,block operation 的执行队列,有系统分配,它是并行操作,main queue add operation , 只是 queue 调用了 operation 的start 方法。

GCD vs NSOperation

GCD is a low-level C-based API.
NSOperation and NSOperationQueue are Objective-C classes.
NSOperationQueue is objective C wrapper over GCD. If you are using NSOperation, then you are implicitly using Grand Central Dispatch.

GCD advantage over NSOperation:
i. implementation
For GCD implementation is very light-weight
NSOperationQueue is complex and heavy-weight

NSOperation advantages over GCD:

i. Control On Operation
you can Pause, Cancel, Resume an NSOperation

ii. Dependencies
you can set up a dependency between two NSOperations
operation will not started until all of its dependencies return true for finished.

iii. State of Operation
can monitor the state of an operation or operation queue. ready ,executing or finished

iv. Max Number of Operation
you can specify the maximum number of queued operations that can run simultaneously

When to Go for GCD or NSOperation
when you want more control over queue (all above mentioned) use NSOperation and for simple cases where you want less overhead (you just want to do some work "into the background" with very little additional work) use `GCD

gcd operation thread

你可能感兴趣的:(GCD NSOperation 拾遗)