This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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 ]
"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
Joined: Jul 06, 2001
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