aspose file tools*
The moose likes Servlets and the fly likes Concurrent access on servlet instance variables... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Concurrent access on servlet instance variables..." Watch "Concurrent access on servlet instance variables..." New topic
Author

Concurrent access on servlet instance variables...

SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
I know this issue has been discussed before, but I have a new question. Lets say we have a servlet that defines a single, non-static instance variable that is an object, not a primitive type. Lets also say that this servlet has a method called "test()", which does nothing other than invoking a method on the servlets instance variable.
Now, if two thread access the servlet and call test() concurrently, what happens when the test() method invokes a method on the instance variable? Would each thread get its own copy of the method invoked on the instance variable?
I'm asking because I have a servlet that loads a class dynamically and creates a single instance of the class then stores a reference to the instance into an instance varaible of the servlet, so I'm concerned about having only one instance of this class that multiple clients can access.
thanks,
SAF
[ January 11, 2002: Message edited by: SAFROLE YUTANI ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
"Now, if two thread access the servlet and call test() concurrently, what happens when the test() method invokes a method on the instance variable? Would each thread get its own copy of the method invoked on the instance variable?
"
Threads don't get "copies" of methods - however, local variables in the method are unique to each Thread. If I understand what you are saying, your code in test() should synchronize on some "flag" object that is created when the servlet is initialized.
Bill
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Yeah, that's what I mean. I meant to say that each thread gets a copy of the local variables in the test() method, that I understand. Now, since the test() method invokes a method on an object that happens to be a global instance variable of the servlet, each thread will get their own copies of local variable within the methods being invoked on that object as well, right? I'm sure this is the case, but I wanted to make sure.
SAF
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent access on servlet instance variables...