1

这不是任何形式的任务。我正在学习 Scala 和 Scala 并发,最好是使用 Akka Actors。我这样做的一种方法是提出一些简单的程序设计并尝试提出一个涉及应用于设计的参与者/并发性的实现:

我要编写的程序是一个储蓄银行账户应用程序,它有一个输入组件和输出组件。
XYZ 银行向客户提供开立储蓄账户的计划
1) 客户 ABC 在开户时被要求提供他/她的个人详细信息
2) 客户可以将钱存入他/她的账户
3) 客户可以提款他/她账户里的钱
4) 如果账户余额低于 100 美元,则通知客户。(例如,当客户在应用程序的输入画面中选择选项 3 时,他/她会被告知当时的账户余额低于 $100,因此无法提款,除非他们存入更多的钱。)
5 )应用程序应能够维护客户的交易历史记录并显示交易历史记录(例如:当应用程序显示输入屏幕时,其中一个选项是 - 显示事务历史记录。 ,应用程序应显示余额)
程序的输入部分由命令行上显示的 4 个选项组成:1. 创建帐户 2. 存款 3. 取款。4.显示交易历史


现在,我现在想要完成的是理解和识别:
1)在这样的程序中,Akka 演员/Scala 并发库的用例是什么?如果有任何角色可以成为此应用程序中的并发角色,该怎么办?
我可以最好地利用哪些 Scala 集合库?

4

1 回答 1

0

我希望这可以解决问题: http ://alexminnaar.com/introduction-to-the-multithreading-problem-and-the-akka-actor-solution.html

注意:我刚刚注意到这篇文章很旧。我将在 2018 年发布答案。

更新1:(来自链接的解释,在评论中提到后)

这是多种可能策略中的一种方法。

使用了两个 Actor

  1. BankAccount - 管理余额

        object WireTransfer {
    
        case class Transfer(from: ActorRef, to: ActorRef, amount: BigInt)
    
        case object Done
    
        case object Failed
    
        }
    
        //actor implementing the actions of a wire transfer between two bank account actors
        class WireTransfer extends Actor {
    
        import WireTransfer._
    
        def receive = LoggingReceive {
        //If Transfer message is received, send withdraw message to 'from' and wait for reply
        case Transfer(from, to, amount) =>
        from ! BankAccount.Withdraw(amount)
        context.become(awaitFrom(to, amount, sender))
        }
    
        //If Withdraw was successful, send deposit to other bank account actor, or else give them a failure message
        def awaitFrom(to: ActorRef, amount: BigInt, customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        to ! BankAccount.Deposit(amount)
        context.become(awaitTo(customer))
        case BankAccount.Failed =>
        customer ! Failed
        context.stop(self)
        }
    
        //If deposit was successful, send 'Done' to original actor that sent Transfer message
        def awaitTo(customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        customer ! Done
        context.stop(self)
        }
        }
    
  2. 电汇 - 将余额从一个帐户转移到另一个帐户。

        object WireTransfer {
    
        case class Transfer(from: ActorRef, to: ActorRef, amount: BigInt)
    
        case object Done
    
        case object Failed
    
        }
    
        //actor implementing the actions of a wire transfer between two bank account actors
        class WireTransfer extends Actor {
    
        import WireTransfer._
    
        def receive = LoggingReceive {
        //If Transfer message is received, send withdraw message to 'from' and wait for reply
        case Transfer(from, to, amount) =>
        from ! BankAccount.Withdraw(amount)
        context.become(awaitFrom(to, amount, sender))
        }
    
        //If Withdraw was successful, send deposit to other bank account actor, or else give them a failure message
        def awaitFrom(to: ActorRef, amount: BigInt, customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        to ! BankAccount.Deposit(amount)
        context.become(awaitTo(customer))
        case BankAccount.Failed =>
        customer ! Failed
        context.stop(self)
        }
    
        //If deposit was successful, send 'Done' to original actor that sent Transfer message
        def awaitTo(customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        customer ! Done
        context.stop(self)
        }
        }
    
于 2018-06-14T12:52:27.723 回答