Scala futures creating infinity threads in actors

2019-08-31 03:44发布

问题:

I am using futures and timeouts inside an actor to avoid system to wait to long for a command to execute:

  val _output = future { "myCommand.sh" !! }

  output = try {
    Await.result(_output, 3 minutes)
  } catch {
    case e: TimeoutException => {
      LOG.warn(s"Timeout for command. Will return empty.")
      ""
    }
  }

  System.out.println("Number of active threads from the given thread: " + Thread.activeCount());

I run this code hundreds of times from different actors. I am noticing that the number of running threads is increasing infinitely. The "myCommand.sh" usually take a few seconds to run. Why this happens? Is there any limit for using futures?

edit 1:

After some time running, all processes that took 1 or 2 seconds to run start logging TIMEOUT.