Hi, Is it possible for a Thread to access a local variable created by other Thread? For example, Thread x access a method and create a local variable. Thread x has not finished, but then Thread y access the same method and also create a local variable. Is it possible for Thread y to access the local variable created by Thread x?
Joined: Feb 28, 2003
Quite simply, no.
Joined: Nov 22, 2008
NO. local variables and method arguments (including the object "references") are kept in stack! and each method call gives rise to another include to the stack.
Joined: Sep 02, 2003
If the variable is declared using ThreadLoacal class, it is specific to the thread. Otherwise(I think) we can access thread variables.
author and iconoclast
The purpose of the ThreadLocal class is indeed to create variables that are specific to a thread, but it's used for member or static variables. As has already been (correctly) stated in this thread, the local variables of a method, static or not, are unique to that single method invocation, and are never shared with any other invocation on the same or any other thread.