NSCondition

前端之家收集整理的这篇文章主要介绍了NSCondition前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@UIApplicationMain

class AppDelegate: UIResponder,UIApplicationDelegate {


var window: UIWindow?


var product = [String]()

let condition = NSCondition()

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// Override point for customization after application launch.

NSThread.detachNewThreadSelector("consume",toTarget: self,withObject: nil)

NSThread.detachNewThreadSelector("create",withObject: nil)

return true

}

func create() {

condition.lock()

print("create a product")

product.append("*")

condition.signal()

condition.unlock()

}

func consume() {

condition.lock()

while product.count == 0 {

print("waiting for create product")

condition.wait()

}

print("consume a product")

product.removeFirst()

condition.unlock()

}

}
原文链接:https://www.f2er.com/swift/325103.html

猜你在找的Swift相关文章