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
| 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{ | |
| val accounts = Map("from" -> new Account("from", 500), "to" -> new 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
| def main(args: Array[String]): Unit = { | |
| import java.util.concurrent._ | |
| import scala.util.Random._ | |
| val executor = Executors.newFixedThreadPool(2) | |
| for(_ <- 1 to 10000){ | |
| executor.submit(new Runnable(){ | |
| def run = { | |
| if(nextInt(2) % 2 == 0) Bank.transfer("from", "to", nextInt(50)) | |
| else Bank.transfer("to", "from", nextInt(50)) | |
| } |
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
| class Account(val accountNo: String, var balance: Int){ | |
| def credit(amount: Int) = balance = balance + amount | |
| def debit(amount: Int) = balance = balance - amount | |
| } | |
| object Bank{ | |
| val accounts = Map("from" -> new Account("from", 500), "to" -> new Account("to", 500) ) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { |
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
| object Circle{ | |
| def circleAtOrigin = Circle(0, 0, _: Double) | |
| } | |
| case class Circle(x: Double, y: Double, radius: Double) | |
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
| def fiftyCircle = Circle(_:Double, _:Double, 50) |
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
| object Rectangle{ | |
| def rectangleAtOrigin = new Rectangle(0, 0, _: Double, _:Double) | |
| } | |
| class Rectangle(x1: Double, y1: Double, x2: Double, y2: Double) |
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
| circleAtOrigin(25) |
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
| def circleAtOrigin = Circle(0, 0, _: Double) |
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
| object Circle{ | |
| def circleAtOrigin = Circle(0, 0, _: Double) | |
| } | |
| case class Circle(x: Double, y: Double, radius: Double) |
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
| case class CircleAtOrigin(override val radius: Double) extends Circle(0, 0, radius) |