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?
Dana Hanna
Ranch Hand
Joined: Feb 28, 2003
Posts: 227
posted
0
Quite simply, no.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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.
Idly Vada
Ranch Hand
Joined: Sep 02, 2003
Posts: 135
posted
0
If the variable is declared using ThreadLoacal class, it is specific to the thread. Otherwise(I think) we can access thread variables.
Ernest Friedman-Hill
author and iconoclast
Marshal
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.