"why are local variables thread safe in java" Code Answer

1

when you create a thread it will have its own stack created. two threads will have two stacks and one thread never shares its stack with other thread.

all local variables defined in your program will be allocated memory in stack (as jatin commented, memory here means, reference-value for objects and value for primitive types) (each method call by a thread creates a stack frame on its own stack). as soon as method execution is completed by this thread, stack frame will be removed.

there is great lecture by stanford professor in youtube which may help you in understanding this concept.

By Bird on June 4 2022

Answers related to “why are local variables thread safe in java”

Only authorized users can answer the Search term. Please sign in first, or register a free account.