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.
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.
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.
Akka Actors is a very popular and widely used framework which implements Actors (or for simplicity, lightweight threads) on the JVM.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With