美文网首页
swift3 GCD学习

swift3 GCD学习

作者: ChoiKarl | 来源:发表于2017-01-10 01:16 被阅读26次

    这篇文章对自定义队列的串并行,优先级,定时器和workItem讲的更为详细,有兴趣的可以直接去看

    GCD精讲(Swift 3)

    队列串行和并行

      func dispatchQueueAttributes() {
        let queue = DispatchQueue(label: "myQueue")
        self.queue = queue
        queue.async {
          for i in 0 ... 5 {
            print("🐖\(i)")
          }
        }
        
        queue.async {
          for i in 0 ... 5 {
            print("😁\(i)")
          }
        }
      }
    
    Paste_Image.png

    执行效果跟预期的一样,串行执行.如果想要并行的话,需要在初始化队列的时候,加上一个参数.

      func dispatchQueueAttributes() {
        // 自定义一个队列
        let queue = DispatchQueue(label: "并行队列", attributes: .concurrent)
        self.queue = queue
        queue.async {
          for i in 0 ... 5 {
            print("🐖\(i)")
          }
        }
        
        queue.async {
          for i in 0 ... 5 {
            print("😁\(i)")
          }
        }
      }
    
    Paste_Image.png

    这样就打到了一个自定义队列并行的效果.

    参数 attributes是DispatchQueue.Attributes类型

    DispatchQueue.Attributes
    

    有两个值

    // 这个上面已经用了,是让队列并行
     public static let concurrent: DispatchQueue.Attributes
    
    // 这个是让队列先不会执行,需要手动调用队列才会去执行
     public static let initiallyInactive: DispatchQueue.Attributes
    
      var queue: DispatchQueue?
      func dispatchQueueAttributes() {
        // 自定义一个队列
        let queue = DispatchQueue(label: "并行队列", attributes: .initiallyInactive)
        self.queue = queue
        queue.async {
          for i in 0 ... 5 {
            print("🐖\(i)")
          }
        }
        
        queue.async {
          for i in 0 ... 5 {
            print("😁\(i)")
          }
        }
      }
      
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        queue?.activate()
      }
    

    定义一个队列, attributes设置为. initiallyInactive, 这个时候队列不会被立马执行,需要调用队列的activate(),队列的任务才会被开始执行, 默认还是串行执行的.

    Paste_Image.png

    如果希望队列需要并行执行,又希望是自己手动调用,可以这样初始化队列.(iOS10以后才能用)

     var queue: DispatchQueue?
      func dispatchQueueAttributes() {
        // 自定义一个队列
        let queue = DispatchQueue(label: "并行队列", attributes: [.initiallyInactive, .concurrent])
        self.queue = queue
        queue.async {
          for i in 0 ... 5 {
            print("🐖\(i)")
          }
        }
        
        queue.async {
          for i in 0 ... 5 {
            print("😁\(i)")
          }
        }
      }
      
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        queue?.activate()
      }
    
    Paste_Image.png

    总结:

    • attributes: 默认为串行
    • concurrent: 并行
    • initiallyInactive: 先不执行,需要主动调用队列才会执行(串行执行)
    • [.concurrent, .initiallyInactive] // 主动调用执行(并行执行)

    队列的优先级

    创建两个队列异步执行,谁先执行完事靠优先级决定,谁的优先级高,CUP就会给那个队列分配的资源就多,就会提前完成

    设置优先级是这个设置:

    DispatchQoS
    

    优先级顺序,从高到低

    userInteractive
    userInitiated
    default
    utility
    background
    unspecified
    

    关于队列优先级的官方文档

    在创建队列和使用系统管理的全局队列都可以设置优先级.

      func dispatchQoS() {
        let queue1 = DispatchQueue(label: "队列1", qos: .userInteractive)
        let queue2 = DispatchQueue(label: "队列2", qos: .utility)
        queue1.async {
          for i in 0 ... 5 {
            print("🐖\(i)")
          }
        }
        
        queue2.async {
          for i in 0 ... 5 {
            print("😁\(i)")
          }
        }
        
        let globalQueue = DispatchQueue.global(qos: .background)
        globalQueue.async {
          for i in 0 ... 5 {
            print("🐑\(i)")
          }
        }
      }
    
    Paste_Image.png

    workItem

     var workItem: DispatchWorkItem?
     workItem = DispatchWorkItem {
       print("执行")
    }
    
     // workItem执行完成会在指定的线程回调这个block
     workItem?.notify(queue: .main, execute: { 
         print("执行完成")
     })
    
     // 执行
     workItem?.perform()
    

    相关文章

      网友评论

          本文标题:swift3 GCD学习

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