Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Are Local variables Thread Safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Are Local variables Thread Safe?" Watch "Are Local variables Thread Safe?" New topic
Author

Are Local variables Thread Safe?

Suni Kr
Greenhorn

Joined: Oct 14, 2004
Posts: 11
Hi everyone,
I know that variables declared inside a service method(local variables) are thread safe. Can some one give me an example and explain how are the variables thread safe.

Thank you,
Suni.


SCJP,SCWCD,SCBCD
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Simply put, there's just nobody else out there that has access to the variable once you've defined it locally.

Now, if you did something crazy like telling everybody else (threads) in your program about "o", then you would have a possible synchronization issue. But until that happens, local variables are nothing to worry about.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Simple Funda - each thread has their own copy of local variables, hence threadsafe


Sanjeev Kaushik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Local variables Thread Safe?
 
Similar Threads
thread
Help w/SingleThreadModel
Thread safety
request atributes are always thread-safe?
Threads and local variables