Sobes.tech
Junior — Middle

Where exactly in Java memory are thread objects stored?

sobes.tech AI

Answer from AI

In Java, thread objects (instances of the Thread class) are stored in the heap. The heap is a memory area where all objects are placed during program execution.

Each thread also has its own stack, where local variables and the execution context of that thread's methods are stored.

In summary:

  • The Thread object is in the heap.
  • The call stack of a specific thread is in a separate memory area allocated for that thread.

For example, when creating a new thread:

Thread t = new Thread(() -> {
    // thread code
});
t.start();

The t object is in the heap, and the stack for executing the lambda expression is allocated separately for this thread.