NSCondition

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

@UIApplicationMain

class AppDelegate: UIResponder,UIApplicationDelegate {


@H_403_15@ var window: UIWindow?


@H_403_15@ var product = [String]()

@H_403_15@ let condition = NSCondition()

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

// Override point for customization after application launch.

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

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

return true

@H_403_15@ }

@H_403_15@ func create() {

condition.lock()

print("create a product")

product.append("*")

condition.signal()

condition.unlock()

@H_403_15@ }

@H_403_15@ func consume() {

condition.lock()

@H_403_15@ while product.count == 0 {

print("waiting for create product")

@H_403_15@ condition.wait()

@H_403_15@ }

print("consume a product")

product.removeFirst()

condition.unlock()

@H_403_15@ }

}

猜你在找的Swift相关文章