Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

은행창구 매니저 [Step3] 토이, 쥬봉이 #330

Open
wants to merge 9 commits into
base: ic_10_toy123
Choose a base branch
from
25 changes: 22 additions & 3 deletions BankManager/Sources/BankManager/Model/BankManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,28 @@ public struct BankManager<BankClerk: CustomerReceivable> {
customerQueue.enqueue(customer)
}

public func assignCustomer(to bankClerk: BankClerk) {
while let customer = customerQueue.dequeue() as? BankClerk.Customer {
bankClerk.receive(customer: customer)
public func assignCustomer(to bankClerk: [Banking: BankClerk]) {
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: 2)
let depositQueue = DispatchQueue(label: "depositQueue", attributes: .concurrent)
let loanQeueue = DispatchQueue(label: "loanQueue")

while let customer = customerQueue.dequeue() as? BankClerk.Customer,
let banking = customer.banking {
switch banking {
case .deposit:
depositQueue.async(group: group) {
semaphore.wait()
bankClerk[banking]?.receive(customer: customer)
semaphore.signal()
}
case .loan:
loanQeueue.async(group: group) {
bankClerk[banking]?.receive(customer: customer)
}
}
Comment on lines +12 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한번에 최대 실행될 수 있는 작업의 수는 3개인데, 생성된 쓰레드의 수는 3개보다 훨씬 많네요.
image

총 3개의 쓰레드만 생성하고싶다면 어떻게 해야할까요?

}

group.wait()
}
}
11 changes: 11 additions & 0 deletions BankManager/Sources/BankManager/Model/Banking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Banking.swift
//
//
// Created by jyubong, Toy on 11/22/23.
//

public enum Banking: String, CaseIterable {
case deposit = "예금"
case loan = "대출"
}
Comment on lines +8 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CustomStringConvertible이라는 프로토콜을 사용해도 좋을 것 같아요.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
public protocol CustomerNumbering {
var number: UInt { get }
var banking: Banking? { get }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 옵셔널일 필요가 있을까요? randomElement()가 옵셔널을 반환한다면, 기본값을 지정해줘도 좋을 것 같아요.

}
4 changes: 2 additions & 2 deletions BankManagerConsoleApp/BankManagerConsoleApp/Model/Bank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BankManager

struct Bank: BankBusinesable {
private let bankManager = BankManager<BankClerk>()
private let bankClerk = BankClerk()
private let bankClerks: [Banking: BankClerk] = [.deposit: BankClerk(work: .deposit), .loan: BankClerk(work: .loan)]
private let customerNumber: UInt

init(customerNumber: UInt) {
Expand All @@ -22,7 +22,7 @@ struct Bank: BankBusinesable {

let startTime = CFAbsoluteTimeGetCurrent()

bankManager.assignCustomer(to: bankClerk)
bankManager.assignCustomer(to: bankClerks)

let finishTime = CFAbsoluteTimeGetCurrent() - startTime
let time = String(format: "%.2f", finishTime)
Expand Down
17 changes: 13 additions & 4 deletions BankManagerConsoleApp/BankManagerConsoleApp/Model/BankClerk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import Foundation
import BankManager

struct BankClerk: CustomerReceivable {
private let pace = 0.7
private let work: Banking
private var pace: Double {
switch work {
case .deposit:
return 0.7
case .loan:
return 1.1
}
}
Comment on lines +11 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 이 정보가 은행원보다는 업무에 들어가야한다고 생각해요. 현재 은행원에 따라 속도차이가 나는게 아니라, 업무에 따라 속도 차이가 나기 때문이에요.

private let startWork: (String) -> Void
private let endWork: (String) -> Void

init(startWork: @escaping (String) -> Void = { print($0) }, endWork: @escaping (String) -> Void = { print($0) }) {
init(work: Banking, startWork: @escaping (String) -> Void = { print($0) }, endWork: @escaping (String) -> Void = { print($0) }) {
self.work = work
self.startWork = startWork
self.endWork = endWork
}

func receive(customer: Customer) {
let number = customer.number

startWork("\(number)번 고객 업무 시작")
startWork("\(number)번 고객 \(work.rawValue)업무 시작")
Thread.sleep(forTimeInterval: pace)
endWork("\(number)번 고객 업무 종료")
endWork("\(number)번 고객 \(work.rawValue)업무 종료")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import BankManager

struct Customer: CustomerNumbering {
let number: UInt
let banking = Banking.allCases.randomElement()
}