newThread{ overridedefrun(): Unit ={ Thread.sleep(200) status = true println("## Status field to true") } }.start()
newThread{ overridedefrun(): Unit ={ while (!status){ println("Status field is false") Thread.sleep(50) } println("Status filed is true") } }.start()
}
运行结果如下:
1 2 3 4 5 6 7 8
[info] Running excercises.chapter15.Ex06 Status field is false Status field is false Status field is false Status field is false ## Status field to true Status filed is true [success] Total time: 23 s, completed May27, 20199:55:17PM
... @annotation.implicitNotFound(msg = "No implicit view available from ${T1} => ${R}.") traitFunction1[@specialized(scala.Int, scala.Long, scala.Float, scala.Double) -T1, @specialized(scala.Unit, scala.Boolean, scala.Int, scala.Float, scala.Long, scala.Double) +R] extendsAnyRef{ self => /** Apply the body of this function to the argument. * @return the result of function application. */ defapply(v1: T1): R ...
... @inlinefinaloverridedefforeach[@specialized(Unit) U](f: Int => U) { // Implementation chosen on the basis of favorable microbenchmarks // Note--initialization catches step == 0 so we don't need to here if (!isEmpty) { var i = start while (true) { f(i) if (i == lastElement) return i += step } } } ...
objectEx10extendsApp{ @elidable(ALL) deffactorial(n: Int): Int = { assert(n >= 0) if (n <= 0) 1else n * factorial(n-1) }
println(factorial(-1)) }
编译异常如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java ... Exception in thread "main" java.lang.AssertionError: assertion failed at scala.Predef$.assert(Predef.scala:208) at excercises.chapter15.Ex10$.factorial(Ex10.scala:7) at excercises.chapter15.Ex10$.delayedEndpoint$excercises$chapter15$Ex10$1(Ex10.scala:11) at excercises.chapter15.Ex10$delayedInit$body.apply(Ex10.scala:5) at scala.Function0.apply$mcV$sp(Function0.scala:39) at scala.Function0.apply$mcV$sp$(Function0.scala:39) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17) at scala.App.$anonfun$main$1$adapted(App.scala:80) at scala.collection.immutable.List.foreach(List.scala:392) at scala.App.main(App.scala:80) at scala.App.main$(App.scala:78) at excercises.chapter15.Ex10$.main(Ex10.scala:5) at excercises.chapter15.Ex10.main(Ex10.scala)