File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Thread Safe access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread Safe access" Watch "Thread Safe access" New topic

Thread Safe access

Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
I have heard people talking about creating only local variables in doPost,doGet methods for Thread Safe access. In some situations I need to call other methods from doPost, but beacuse of thread safty concerns I am passing all the variables to the called methods.
Like for instance in doPost

Is there any way to make vector accessible to other methods without passing as one of the input parameters and comprimising Thread Safety ? I know I can implement SingleThreadModel but I am looking if there is any other way I can do it.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you can synchronize access to those variables.

But there's also nothing wrong with your approach either.
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Use the method that you indicated. An alternative would be to synchronize the method, but this may produce a negative impact on performance. There is absolutely nothing wrong with your approach. You should always try to use automatic (local) variables when using servlets.
good luck,
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
What does one do when all copies of a running servlet need to know and update a set or list of values? What modifiers are on the variables, and how is the best method to synchronize it to keep the updates safe? For example, doing writes to a data or log file?

CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
I agree. Here's the link:
subject: Thread Safe access
It's not a secret anymore!