I'm wondering whether before start()
method is called, instances of Thread
are merely a standard Java objects or if some special JVM/OS level resources are allocated right from the moment they are created (like for example pre-allocated memory for their stack etc). Based on a preliminary research, they seem like ordinary objects, but it would be awesome if some JVM experts could confirm this.
I'm mostly concerned regarding previously mentioned thread's stack memory, as the JVM spec states "Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread". I wonder if "created" here refers to a call to start()
method or a Thread
instance being constructed (I'm honestly not up2date with the exact relation between OS threads, JVM threads and Thread
objects, hence my confusion. Also I think I might have heard it changed a bit during last few years, but I can't recall exactly). Furthermore, I'm guessing, that even in the case some OS virtual memory is allocated at the moment a Thread
instance is constructed, actual physical memory pages for its stack will be allocated only after the thread starts to use, which, I guess again, is only after it is start()
-ed.
This is the "change I might have heard about": jep-425 introduced in java-19. It changes the mapping between OS threads and JVM threads to M:N. I'm however still not sure about the mapping between Thread
objects and JVM threads (I'm almost sure that mapping between start()
-ed Thread
objects and JVM threads is 1:1, but not sure if a JVM thread is created on Thread
instance creation or on start()
. I've asked another question regarding this)