This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private val map = new java.util.HashMap[String, Account]() | |
| map.put("from", Account("from", 500)) | |
| map.put("to", Account("to", 500)) | |
| val accountsRef = Ref(map) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { | |
| atomic{ implicit t => | |
| var accounts = accountsRef() | |
| val (from, to) = (accounts.get(fromNo), accounts.get(toNo)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.stm._ | |
| case class Account(accountNo: String, balance: Int){ | |
| def credit(amount: Int) = this.copy(balance = (balance + amount)) | |
| def debit(amount: Int) = this.copy(balance = (balance - amount)) | |
| } | |
| object Bank{ | |
| val accountsRef = Ref(Map("from" -> Account("from", 500), "to" -> Account("to", 500) )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.stm._ | |
| class Account(val accountNo: String, beginningBalance: Int){ | |
| private var _balance = beginningBalance | |
| def balance = this.synchronized{ _balance } | |
| def credit(amount: Int) = this.synchronized{ _balance = _balance + amount } | |
| def debit(amount: Int) = this.synchronized { _balance = _balance - amount } | |
| } | |
| object Bank{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private val map = new java.util.HashMap[String, Account]() | |
| map.put("from", Account("from", 500)) | |
| map.put("to", Account("to", 500)) | |
| val accountsRef = Ref(map) | |
| val hashMap = atomic{ implicit t => accountsRef() } | |
| crazyThreads(hashMap) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private val map = new java.util.HashMap[String, Account]() | |
| map.put("from", Account("from", 500)) | |
| map.put("to", Account("to", 500)) | |
| val accountsRef = Ref(map) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { | |
| atomic{ implicit t => | |
| var accounts = accountsRef() | |
| val (from, to) = (accounts.get(fromNo), accounts.get(toNo)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val shouldApplyTransferFees: Boolean = atomic{ implicit t => | |
| var accounts = accountsRef() | |
| val (from, to) = (accounts(fromNo), accounts(toNo)) | |
| if(from.balance > amount){ | |
| accounts = accounts + (to.accountNo -> to.credit(amount)) | |
| accounts = accounts + (from.accountNo -> from.debit(amount)) | |
| accountsRef() = accounts | |
| true | |
| }else false | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| atomic{ implicit t => | |
| var accounts = accountsRef() | |
| val (from, to) = (accounts(fromNo), accounts(toNo)) | |
| if(from.balance > amount){ | |
| accounts = accounts + (to.accountNo -> to.credit(amount)) | |
| accounts = accounts + (from.accountNo -> from.debit(amount)) | |
| accountsRef() = accounts | |
| applyTransferFees() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.stm._ | |
| case class Account(accountNo: String, balance: Int){ | |
| def credit(amount: Int) = this.copy(balance = (balance + amount)) | |
| def debit(amount: Int) = this.copy(balance = (balance - amount)) | |
| } | |
| object Bank{ | |
| val accountsRef = Ref(Map("from" -> Account("from", 500), "to" -> Account("to", 500) )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.actors._ | |
| import Actor._ | |
| object Bank extends Actor{ | |
| private var accounts = Map("from" -> Account("from", 500), "to" -> Account("to", 500) ) | |
| def act = { | |
| loop{ | |
| react{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.actors._ | |
| import Actor._ | |
| object Bank{ | |
| val accounts = Map("from" -> new Account("from", 500), "to" -> new Account("to", 500) ) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { | |
| val (from, to) = (accounts(fromNo), accounts(toNo)) | |
| val balance = (from !? Balance).asInstanceOf[Int] |