Results 1 to 7 of 7
  1. #1
    samson is offline Member
    Join Date
    Mar 2007
    Posts
    16
    Rep Power
    0

    Default Are Local variables thread safe ?

    I have something like this
    Java Code:
    public  class ThreadTest {
    
    public static void testAgain(){
     ThreadTest threadTest = new ThreadTest();
    }
    Do I have to put the method testAgain as synchronised ?

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Local variables are threadsafe--each thread gets its own copy of locals.

    The objects that the variables point to may not be threadsafe--for instance, if some other thread has references to the same objects.

  3. #3
    Ashish is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    You needn't worry about multithreaded access to local variables, method parameters, and return values, because these variables reside on the Java stack. In the JVM, each thread is awarded its own Java stack. No thread can see or use any local variables, return values, or parameters belonging to another thread.

    but you need only be concerned with instance and class variables when you worry about thread safety. Because all threads share the same heap, and the heap is where all instance variables are stored, multiple threads can attempt to use the same object's instance variables concurrently.

  4. #4
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    thanks Ashish

  5. #5
    rato is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi All

    I want to know whether in the following case my code is thread safe or not

    I am having one Parent class in that class i am having global varible of another class Say B.

    In the method from Parent class I am calling method on the object B.

    above method is taking the arraylist as parameter to the method.

    this Arraylist is modifying in the Parent Class Method and I am Iterating over the Arraylist in the B class method which is parameter to the method.

    In above case arraylist is threadSafe ?

    Thanks in advance

  6. #6
    rato is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    One more thing i have forgotten

    Both classes are singleton only

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    rato, please do not hijack old threads with a new question. Rather, please ask your question in its own thread and if desired link to this thread. I'm locking this thread. Please private message me if you have any questions.

Similar Threads

  1. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM
  2. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  3. Struts framework. Is this thread safe?
    By JavaAl2 in forum Web Frameworks
    Replies: 1
    Last Post: 01-17-2008, 04:01 AM
  4. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 02:06 AM
  5. Replies: 0
    Last Post: 11-10-2007, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •