Skip to content

Future's Flatmap is not stacksafe #11256

Closed
@He-Pin

Description

@He-Pin
@RunWith(classOf[JUnit4])
class Stacksafe {

  @Test
  def testFib(): Unit = {
    implicit val ex: ExecutionContext = new ExecutionContext {
      override def execute(runnable: Runnable): Unit = runnable.run()
      override def reportFailure(cause: Throwable): Unit = cause.printStackTrace()
    }

    def fib(n: Int, a: Long = 0, b: Long = 1): Future[Long] =
      Future(a + b).flatMap { b2 =>
        if (n > 0)
          fib(n - 1, b, b2)
        else
          Future(b2)
      }

    val future = fib(1000)
    println(Await.result(future,Duration.Inf))
  }
}

It's ok with 100,but not 1000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions