Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Member variables within servlet application - are they threadsafe?

Reply
Thread Tools

Member variables within servlet application - are they threadsafe?

 
 
Simon Eichenauer
Guest
Posts: n/a
 
      06-25-2007
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



 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      06-25-2007

"Simon Eichenauer" <(E-Mail Removed)> wrote in message
news:f5pdb7$7nc$(E-Mail Removed)-hagen.de...
| 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.

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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
servlet help - redirecting from within a framed servlet ppcguy Java 1 08-08-2005 03:03 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments