美文网首页
自己总结NSOperation

自己总结NSOperation

作者: FredYJH | 来源:发表于2017-02-22 18:02 被阅读8次
    func loadWithUrlStr(urlStr:String){
        let queue = OperationQueue()
        
        
        
        print("111")
        let blockQueue1 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            
            print("blockQueue1%@",Thread.current)
        }
        let blockQueue2 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue2%@",Thread.current)
        }
        
        let blockQueue3 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue3%@",Thread.current)
        }
        let blockQueue4 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue4%@",Thread.current)
        }
        let blockQueue5 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue5%@",Thread.current)
        }
        let blockQueue6 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue6%@",Thread.current)
        }
        
        let blockQueue7 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue7%@",Thread.current)
        }
        let blockQueue8 = BlockOperation {
            for _ in 1 ..< 500000 {
                
            }
            print("blockQueue8%@",Thread.current)
        }
        print("2222")
        blockQueue7.addDependency(blockQueue5)
        blockQueue5.addDependency(blockQueue2)
    
        blockQueue2.queuePriority = .high
        blockQueue5.queuePriority = .high
        blockQueue7.queuePriority = .veryHigh
    
        queue.addOperations([blockQueue1,blockQueue2,blockQueue3,blockQueue4,blockQueue5,blockQueue6,blockQueue7,blockQueue8], waitUntilFinished: true)
        // 注意一点,添加了依赖关系的会被放到后面执行。
        // 没有依赖和优先级:执行的顺序不确定
        // 只有依赖:执行的顺序不确定,有依赖的那个最后执行,只能确定依赖的先后顺序,不能确定其他的顺序
        // 只有优先级:执行优先级高的,其他顺序不确定
        // 有依赖和优先级:先执行依赖,然后执行优先级
        
    }

    相关文章

      网友评论

          本文标题:自己总结NSOperation

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