美文网首页
设置请求并发数

设置请求并发数

作者: 小凡凡520 | 来源:发表于2019-10-11 15:53 被阅读0次
    一、请求超时
    // 设置请求最大并发数
    URLSessionConfiguration().httpMaximumConnectionsPerHost = xxxx
    
    二、多任务无顺序
    • 自动关联
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let group = DispatchGroup()
        
        DispatchQueue.global().async(group: group, execute: {
            print("1")
        })
        DispatchQueue.global().async(group: group, execute: {
            print("2")
        })
        DispatchQueue.global().async(group: group, execute: {
            print("3")
        })
        DispatchQueue.global().async(group: group, execute: {
            print("4")
        })
        DispatchQueue.global().async(group: group, execute: {
            print("5")
        })
        DispatchQueue.global().async(group: group, execute: {
            print("6")
        })
        
        group.notify(queue: DispatchQueue.main) {
            print("end")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let block = BlockOperation {
            print("1")
        }
        block.addExecutionBlock {
            print("2")
        }
        block.addExecutionBlock {
            print("3")
        }
        block.addExecutionBlock {
            print("4")
        }
        block.addExecutionBlock {
            print("5")
        }
        block.addExecutionBlock {
            print("6")
        }
        block.addExecutionBlock {
            print("7")
        }
        
        // 监听任务完成
        block.completionBlock = {
            print("end")
        }
        block.start()
    }
    
    • 手动关联
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let group = DispatchGroup()
        
        group.enter()
        DispatchQueue.global().async {
            print("1")
            group.leave()
        }
        group.enter()
        DispatchQueue.global().async {
            print("2")
            group.leave()
        }
        group.enter()
        DispatchQueue.global().async {
            print("3")
            group.leave()
        }
        group.enter()
        DispatchQueue.global().async {
            print("4")
            group.leave()
        }
        group.enter()
        DispatchQueue.global().async {
            print("5")
            group.leave()
        }
        group.enter()
        DispatchQueue.global().async {
            print("6")
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
            print("end")
        }
    }
    
    三、多任务有顺序
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let block1 = BlockOperation {
            print("1")
        }
        let block2 = BlockOperation {
            print("2")
        }
        let block3 = BlockOperation {
            print("3")
        }
        let block4 = BlockOperation {
            print("4")
        }
        let block5 = BlockOperation {
            print("5")
        }
        block2.addDependency(block1)
        block3.addDependency(block2)
        block4.addDependency(block3)
        block5.addDependency(block4)
        
        block1.start()
        block2.start()
        block3.start()
        block4.start()
        block5.start()
    }
    

    相关文章

      网友评论

          本文标题:设置请求并发数

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