Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 04:53 AM
Member
 
Join Date: Mar 2007
Posts: 16
samson is on a distinguished road
Are Local variables thread safe ?
I have something like this
Code:
public class ThreadTest { public static void testAgain(){ ThreadTest threadTest = new ThreadTest(); }
Do I have to put the method testAgain as synchronised ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2007, 04:55 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-18-2007, 02:49 PM
Member
 
Join Date: Jul 2007
Posts: 4
Ashish is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-09-2007, 08:59 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
thanks Ashish
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The safe way to stop a thread Java Tip java.lang 0 04-09-2008 08:31 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Struts framework. Is this thread safe? JavaAl2 Web Frameworks 1 01-17-2008 05:01 AM
Local Variables for a static method - thread safe? mikeg1z Advanced Java 1 11-16-2007 03:06 AM
is synchronization on method passing local variables as parameters needed reddzer Java Servlet 0 11-10-2007 06:47 PM


All times are GMT +3. The time now is 07:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org