31 lines
705 B
Swift
31 lines
705 B
Swift
import Foundation
|
|
|
|
actor Warehouse {
|
|
var stock: Int = 10
|
|
func takeFromStock(amount: Int, thread: String) {
|
|
let localStock = stock
|
|
stock -= amount
|
|
print(thread)
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct App {
|
|
static func main() async {
|
|
let warehouse = Warehouse()
|
|
|
|
let threadB = Task.detached {
|
|
await warehouse.takeFromStock(amount: 2, thread: "B")
|
|
}
|
|
|
|
let threadA = Task.detached {
|
|
await warehouse.takeFromStock(amount: 1, thread: "A")
|
|
}
|
|
|
|
_ = await threadB.result
|
|
_ = await threadA.result
|
|
|
|
let finalStock = await warehouse.stock
|
|
print("Endgültiger Lagerbestand: \(finalStock) Stück")
|
|
}
|
|
}
|