swift 线程同步

方法一 使用GCD和信号量

@IBAction func requestNetAction(_ sender: Any) {
        
        let group = DispatchGroup();
        let  queueRequest = DispatchQueue.global();
        
        queueRequest.async(group:group){
            let semaphore = DispatchSemaphore(value: 0);
            print("第1个")
            Alamofire.request("https://httpbin.org/get").responseJSON { response in
                semaphore.signal()
            }
            let result = semaphore.wait(timeout: DispatchTime.distantFuture)
            if(result == DispatchTimeoutResult.success)
            {
                print("第1个请求回来")
            }
            
        }
        
        queueRequest.async(group:group){
            let semaphore = DispatchSemaphore(value: 0);
            print("第2个")
            Alamofire.request("https://httpbin.org/get").responseJSON { response in
                semaphore.signal()
            }
            let result = semaphore.wait(timeout: DispatchTime.distantFuture)
            if(result == DispatchTimeoutResult.success)
            {
                print("第2个请求回来")
            }
        }
        
        queueRequest.async(group:group){
            let semaphore = DispatchSemaphore(value: 0);
            print("第3个")
            Alamofire.request("https://httpbin.org/get").responseJSON { response in
                semaphore.signal()
            }
            let result = semaphore.wait(timeout: DispatchTime.distantFuture)
            if(result == DispatchTimeoutResult.success)
            {
                print("第3个请求回来")
            }
        }
        
        group.notify(queue: queueRequest){
            print("请求结束")
        }
        
        print("其他任务");
    }

方法二 使用计数器的方式



@IBAction func requestNetAction(_ sender: Any) {
        
        let array = ["https://httpbin.org/get",
                     "http://test.api.wukongtv.com/setting/init",
                     "http://test.api.wukongtv.com/setting/ad"]
        
        sendBatchRequests(_requests: array) { 
            print("请求结束")
        }
        
        print("其他任务");
    }
    
    func sendBatchRequests(_requests:Array, completionHandler: @escaping ()->()) {
        var finshCount = 0
        let allTaskCount = _requests.count
        
        let queue = DispatchQueue.global();
        queue.async {
            print("+++++"+Thread.current.description)
            print("开始发出请求")
            for request in _requests {
                print("-----"+Thread.current.description)
                print("发出请求:" + request.description)
                Alamofire.request(request).responseJSON { response in
                    print("*****"+Thread.current.description)
                    print("收到请求" + (response.request?.description)! )
                    finshCount += 1
                    if (finshCount == allTaskCount){
                        completionHandler()
                    }
                }
            }
        }
    }

方法三 使用GCD group的enter、leave、notify方法

@IBAction func requestNetAction(_ sender: Any) {
        
        let group = DispatchGroup()
        group.enter()
        print("开始第1个请求")
        Alamofire.request("https://httpbin.org/get").responseJSON { response in
            print("接收第1个请求")
            group.leave()
        }
        
        group.enter()
        print("开始第2个请求")
        Alamofire.request("https://httpbin.org/get").responseJSON { response in
            print("接收第2个请求")
            group.leave()
        }
        
        group.enter()
        print("开始第3个请求")
        Alamofire.request("https://httpbin.org/get").responseJSON { response in
            print("接收第3个请求")
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
            print("任务结束")
        }
        
        print("接着往下跑")
        
    }

你可能感兴趣的:(swift 线程同步)