ios 一道绝佳的面试题

版权声明:转载请标注原文地址。邮箱[email protected] https://blog.csdn.net/Xoxo_x/article/details/82187906

本题考查GCD信号量,线程,函数作用域
请按照计算机的执行程序写出log信息:

import UIKit

class ViewController: UIViewController {

    var d:String?
    // semaphore
    let semaphore = DispatchSemaphore.init(value: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
        doQueueOne(a: "ss")
        doQueueThree()
        doQueueTwo()
        // Do any additional setup after loading the view, typically from a nib.
    }


    func doQueueOne(a:String){
        if let d = d{
            DispatchQueue.main.asyncAfter(deadline: .now()) {
                print(#line)
                self.semaphore.wait()
                print(#line)
                DispatchQueue.init(label: "1235").asyncAfter(deadline: .now() + 4) {
                    print(#line,d)
                }
            }
        }

    }

    func doQueueTwo(){
        DispatchQueue.init(label: "123").asyncAfter(deadline: .now() + 3) {
            print(#line)
            self.semaphore.signal()
            print(#line)
            self.d = "1"
            self.doQueueOne(a:"qq1")
        }
        DispatchQueue.init(label: "234").asyncAfter(deadline: .now() + 1) {
            print(#line)
            self.semaphore.signal()
            print(#line)
            self.d = "2"
            self.doQueueOne(a:"qq2")
        }
    }
    func doQueueThree(){
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            print(#line)
            self.semaphore.wait()
            print(#line)
            self.d = "3"
            self.doQueueOne(a:"qq3")
        }
    }
}

你会发现死锁:

更改如下

import UIKit

class ViewController: UIViewController {


    // data
    var a:String?
    var b:String?
    var c:String?

    var d:String? = "0"
    // semaphore
    let semaphore = DispatchSemaphore.init(value: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
        doQueueOne(a: "ss")
        doQueueThree()
        doQueueTwo()
        // Do any additional setup after loading the view, typically from a nib.
    }


    func doQueueOne(a:String){
        if let d = d{
            DispatchQueue.main.asyncAfter(deadline: .now()) {
                DispatchQueue.init(label: "1235").asyncAfter(deadline: .now() + 4) {
                    print(#line,d)
                }
            }
        }

    }

    func doQueueTwo(){
        DispatchQueue.init(label: "123").asyncAfter(deadline: .now() + 3) {
            self.semaphore.signal()
            self.d = "1"
            self.doQueueOne(a:"qq1")
        }
        DispatchQueue.init(label: "234").asyncAfter(deadline: .now() + 1) {
            self.semaphore.signal()
            self.d = "2"
            self.doQueueOne(a:"qq2")
        }
    }
    func doQueueThree(){
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            self.semaphore.wait()
            self.d = "3"
            self.doQueueOne(a:"qq3")
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Xoxo_x/article/details/82187906