Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Member variables within servlet application - are they threadsafe?

Thread view: 
Simon Eichenauer - 25 Jun 2007 22:50 GMT
Hi,

I´ve got a question about thread safety within servlet applications.
I know that, for example within a servlet or a struts action, you
shouldn´t use member variables because within the virtual machine
only one instance of each servlet / action exists (or at least the member
variables exist only once and aren´t put on the thread stack). But what
about member variables declared within
classes that are used within servlet or action methods? Are they threadsafe?
For example:

Action
execute() {

   Test test = new Test();
   test.setMemberVariable("test");
}

Class Test {
   private String member; // Threadsafe ???

   public void setMemberVariable(String test) {
   member = test;
   }
}

Maybe it´s really stupid but I just couldn´t find any hint.

Regards,
Simon
Matt Humphrey - 26 Jun 2007 00:14 GMT
| Hi,
|
[quoted text clipped - 23 lines]
|
| Maybe it´s really stupid but I just couldn´t find any hint.

Your new test object's reference is held locally to the thread and if you do
not allow the reference to be stored where it could be accessed by another
thread, the object will be threadsafe because other threads cannot access
it. Its methods will only be activated from the original thread.  If you
store the reference in some structure that is accessible to other threads
(static variables, servlet instance variables, any data structure referenced
by a servlet instance variable, etc,) you expose it to being used by two
threads at the same time.  In short, you can't determine threadsafety by
looking at the class--you have to look at how its used.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.