这里是方法的定义,它隐式使用ExecutionContext
,并允许客户端覆盖它。使用两个执行上下文对其进行测试:
val defaultEc = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(5))
线程名称如下:'pool-1-thread-1'到'pool-1-thread-5'
Scala的第二个:
scala.concurrent.ExecutionContext.Implicits.global
线程名称如下:'scala-execution-context-global-11'
客户可以通过以下方式覆盖默认的隐式值:
implicit val newEc = scala.concurrent.ExecutionContext.Implicits.global
不幸的是,只有在没有()
的情况下调用带有隐式的方法时,它才是可重写的:
val r = FutureClient.f("testDefault") //prints scala-execution-context-global-11
不起作用:
val r = FutureClient.f("testDefault")() //still prints: pool-1-thread-1
问题是为什么它会以这种方式工作?因为它使API客户端变得更加复杂
这是运行和播放的完整代码:
object FutureClient {
//thread names will be from 'pool-1-thread-1' to 'pool-1-thread-5'
val defaultEc = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(5))
def f(beans: String)
(implicit executor:ExecutionContext = defaultEc)
: Future[String] = Future {
println("thread: " + Thread.currentThread().getName)
TimeUnit.SECONDS.sleep(Random.nextInt(3))
s"$beans"
}
}
class FutureTest {
//prints thread: pool-1-thread-1
@Test def testFDefault(): Unit ={
val r = FutureClient.f("testDefault")
while (!r.isCompleted) {
TimeUnit.SECONDS.sleep(2)
}
}
//thread: scala-execution-context-global-11
@Test def testFOverridable(): Unit ={
implicit val newEc = scala.concurrent.ExecutionContext.Implicits.global
val r = FutureClient.f("testDefault")
while (!r.isCompleted) {
TimeUnit.SECONDS.sleep(2)
}
}
//prints pool-1-thread-1, but not 'scala-execution-context-global-11'
//cause the client invokes f with () at the end
@Test def testFOverridableWrong(): Unit ={
implicit val newEc = scala.concurrent.ExecutionContext.Implicits.global
val r = FutureClient.f("testDefault")()
while (!r.isCompleted) {
TimeUnit.SECONDS.sleep(2)
}
}
}
我已经讨论了几个相关主题,但是它们与API定义有关,因此这是一个新问题,这些主题未涵盖。
答案 0 :(得分:5)
Scala Patterns To Avoid: Implicit Arguments With Default Values
redirect('Public/profile);
(或f("testDefault")
)表示隐式参数取自隐式上下文。
f("testDefault")(implicitly)
表示您显式指定隐式参数。如果您编写f("testDefault")(newEc)
,则表示您显式指定了隐式参数,但由于未提供该值,因此应从默认值中获取。