ses
ses

Reputation: 13342

scala Future deadlock - fixed 4-thread pool

Digging around concurrecny(again).

Q1: Why it is dead-lock?

implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))

val futures = List( Future{1} )

val result: Future[List[Int]] = Future.sequence(futures)

Await.ready(result, Duration.Inf)

I expected to have similar behaviour when I have 5 future/threads in the pool (more than 4).

Upvotes: 0

Views: 864

Answers (1)

ymonad
ymonad

Reputation: 12090

Maybe it's not a deadlock. I think the main thread is waiting for the thread pool to exit. try calling .shutdown() of the executor service.

import java.util.concurrent.Executors
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Await, Future}

object DeadLockSample1 {
  def main(args: Array[String]) {
    val pool =  Executors.newFixedThreadPool(4)
    implicit val ec = ExecutionContext.fromExecutor(pool)
    Future{1}
    pool.shutdown() // without this, it won't exit.
  }
}

Upvotes: 3

Related Questions