Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads does Akka use?

Tags:

scala

akka

As i know, Akka uses several threads to serve all actors in ActorSystem. I would like to programmatically check, how many threads Akka uses. But i don't want to use profilers or jConsole.

like image 742
pacman Avatar asked Aug 14 '16 11:08

pacman


People also ask

Does Akka use threads?

Behind the scenes, Akka runs actors on real threads and many actors may share one thread. A Actor can create many actors called child actors. Actors interact only through asynchronous messages and never through direct method calls.

Is Akka single threaded?

In Akka, actors are guaranteed to be run in a single-threaded illusion, which means that the Akka framework takes care of threading issues while allowing us to focus on the behavior that needs to be implemented. Actors may only communicate with each other and the outside world by through messages.

Is Akka widely used?

Akka Actors is a very popular and widely used framework which implements Actors (or for simplicity, lightweight threads) on the JVM.

How does Akka handle concurrency?

Akka's approach to handling concurrency is based on the Actor Model. In an actor-based system, everything is an actor, in much the same way that everything is an object in object-oriented design.


2 Answers

An estimate can be made from the configuration since it is usually a factor of the number of cores on the host.

An exact number would come from the thread pools used under the hood. The first issue is getting a handle to the underlying pool from the dispatchers. Note that there can be multiple dispatchers/executors.

The most common is the fork join pool. You can call getActiveThreadCount on the pool to find the number of threads in use. If java's ForkJoinPool is being used, you can call getPoolSize.

like image 100
dres Avatar answered Oct 12 '22 11:10

dres


You can traverse the thread group tree to find the thread groups created by akka, and get the active thread counts.

object ThreadGroupUtils {

  def getThreadGroup: ThreadGroup = Thread.currentThread.getThreadGroup

  def findAncestor(current: ThreadGroup)(isTarget: ThreadGroup => Boolean): Option[ThreadGroup] = {
    Option(current.getParent) match {
      case None =>
        None
      case opt @ Some(parent) if isTarget(parent) =>
        opt
      case Some(parent) =>
        findAncestor(parent)(isTarget)
    }
  }

  def root = findAncestor(getThreadGroup)(_.getParent == null).get

  def main = findAncestor(getThreadGroup)(_.getName == "main").get

  def getSubGroups(parent: ThreadGroup): Seq[ThreadGroup] = {
    val subgroups = new Array[ThreadGroup](parent.activeGroupCount)
    val copied = parent.enumerate(subgroups)
    subgroups.iterator.take(copied).toSeq
  }

  def printThreadGroups(group: ThreadGroup = root, indent: String = ""): Unit = {
    val subgroups =getSubGroups(group)
    println(s"${indent}${group.getName}: ${subgroups.size} subgroups, ${group.activeCount} threads")
    subgroups.foreach(printThreadGroups(_, indent + "    "))
  }
}

ThreadGroupUtils.printThreadGroups()
like image 28
thirstycrow Avatar answered Oct 12 '22 12:10

thirstycrow