|
| 1 | +package s4j.scala.chapter17 |
| 2 | + |
| 3 | +import s4j.scala.chapter12.{Customer, ShoppingBasket} |
| 4 | +import s4j.scala.chapter17.ThreadExample._ |
| 5 | + |
| 6 | +object ThreadExample { |
| 7 | + |
| 8 | + def runInThread(function: () => Unit) { |
| 9 | + new Thread() { |
| 10 | + override def run(): Unit = function() // aka function.apply() |
| 11 | + }.start() |
| 12 | + } |
| 13 | + |
| 14 | + def runInThread2(function: => Unit) { // call-by-name |
| 15 | + new Thread() { |
| 16 | + override def run(): Unit = function // not function() |
| 17 | + }.start() |
| 18 | + } |
| 19 | + |
| 20 | + def runInThread3(group: String, function: => Unit) { |
| 21 | + new Thread(new ThreadGroup(group), () => function).start() |
| 22 | + } |
| 23 | + |
| 24 | + def runInThread4(group: String)(function: => Unit) { |
| 25 | + new Thread(new ThreadGroup(group), () => function).start() |
| 26 | + } |
| 27 | + |
| 28 | + def main(args: Array[String]) { |
| 29 | + runInThread(() => { |
| 30 | + // some long running task |
| 31 | + println("Hello function") |
| 32 | + }) |
| 33 | + |
| 34 | + runInThread2 { |
| 35 | + println("Hello Lazy val") |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/* |
| 41 | + a class representing some kind of UI with 'update' methods |
| 42 | + to update itself |
| 43 | +*/ |
| 44 | +class UI { |
| 45 | + |
| 46 | + def updateUiElements() { |
| 47 | + new Thread() { |
| 48 | + override def run(): Unit = updateCustomerBasket(basket) |
| 49 | + }.start() |
| 50 | + |
| 51 | + new Thread() { |
| 52 | + override def run(): Unit = updateOffersFor(customer) |
| 53 | + }.start() |
| 54 | + |
| 55 | + // more updates, all done in their own threads |
| 56 | + } |
| 57 | + |
| 58 | + def updateUiElements2() { |
| 59 | + runInThread(() => updateCustomerBasket(basket)) |
| 60 | + runInThread(() => updateOffersFor(customer)) |
| 61 | + // more updates, all done in their own threads |
| 62 | + } |
| 63 | + |
| 64 | + def updateUiElements3() { |
| 65 | + runInThread { () => |
| 66 | + updateCustomerBasket(basket) |
| 67 | + } |
| 68 | + runInThread { () => |
| 69 | + updateOffersFor(customer) |
| 70 | + } |
| 71 | + // more updates, all done in their own threads |
| 72 | + } |
| 73 | + |
| 74 | + def updateUiElementsX() { |
| 75 | + runInThread(() => { |
| 76 | + applyDiscountToBasket(basket) |
| 77 | + updateCustomerBasket(basket) |
| 78 | + }) |
| 79 | + runInThread(() => updateOffersFor(customer)) |
| 80 | + // more updates, all done in their own threads |
| 81 | + } |
| 82 | + |
| 83 | + def updateUiElements4() { |
| 84 | + runInThread2 { |
| 85 | + applyDiscountToBasket(basket) |
| 86 | + updateCustomerBasket(basket) |
| 87 | + } |
| 88 | + runInThread2 { |
| 89 | + updateOffersFor(customer) |
| 90 | + } |
| 91 | + // more updates, all done in their own threads |
| 92 | + } |
| 93 | + |
| 94 | + def updateUiElements5() { |
| 95 | + runInThread3("basket", { |
| 96 | + applyDiscountToBasket(basket) |
| 97 | + updateCustomerBasket(basket) |
| 98 | + }) |
| 99 | + runInThread3("customer", |
| 100 | + updateOffersFor(customer) |
| 101 | + ) |
| 102 | + // more updates, all done in their own threads |
| 103 | + } |
| 104 | + |
| 105 | + def updateUiElements6() { |
| 106 | + runInThread4("basket") { |
| 107 | + applyDiscountToBasket(basket) |
| 108 | + updateCustomerBasket(basket) |
| 109 | + } |
| 110 | + runInThread3("customer", |
| 111 | + updateOffersFor(customer) |
| 112 | + ) |
| 113 | + // more updates, all done in their own threads |
| 114 | + } |
| 115 | + |
| 116 | + def applyDiscountToBasket(basket: ShoppingBasket) {} |
| 117 | + def updateCustomerBasket(basket: ShoppingBasket) {} |
| 118 | + def updateOffersFor(customer: Customer) {} |
| 119 | + |
| 120 | + private val basket = new ShoppingBasket() |
| 121 | + private val customer = new Customer("", "") |
| 122 | +} |
0 commit comments