This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Instance variables" Watch "Instance variables" New topic
Author

Instance variables

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 263
Regarding the multi thread issue in the previous discussion I understand that we shouldn't declare istance variables in the servlet.

But what about declaring instance variables in another class, which is called from the servlet? Please see the example below.

If two theads are requesting MyServlet at the same time, are there any risk that thread 2 could overwrite the public instance variables initiated in the class called SessionHandler, when called from the servlet as below?

Please note that a local instance of SessionHandler is created in the servlets doGet method.

Do you see any problems in the code below in an application with many requests?


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3786
    
    1

That looks fine (the principle - I haven't looked at the detail), because the instance of SessionHandler you're creating is local to the doGet method. As long as everything is local to the method then you're safe from threads interacting.


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14456
    
    7

As long as the SessionHandler is an object created and destroyed within the scope of the doGet() method, there's no problem with SessionHandler and threads, because each thread has its own stack and a unique instance of SessionHandler will reside there. Since the difference between class members and instance members is that instance members are unique to each instance instead of being shared between instances like class members are, there's no conflict.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 263
Tim Holloway wrote:As long as the SessionHandler is an object created and destroyed within the scope of the doGet() method, there's no problem with SessionHandler and threads, because each thread has its own stack and a unique instance of SessionHandler will reside there. Since the difference between class members and instance members is that instance members are unique to each instance instead of being shared between instances like class members are, there's no conflict.


So, do we also need to destroy the SessionHandler object manually, something like? If so, why is that?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Instance variables
 
Similar Threads
JSP synchronization
servlet mapping
Memory leak - Buffer size problem?
Synchronization problem with session?!
Could not resolve a persistence unit corresponding to the persistence-context-ref-name