aspose file tools
The moose likes Servlets and the fly likes Servlets - help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Servlets - help" Watch "Servlets - help" New topic
Author

Servlets - help

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hi,
Please have a look at the code fragment ive given below
public class PSPsendDoc extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
...........lines of code
UIS.addElement(..);
UIS.addElement(..);
.................
}
private Vector UIS = new Vector();
}
This servlet of mine can be called by several users at a time. Thus there is a very high probability that this servlet may be called my several clients almost at the same time.
So my question is , does this mean that the vector UIS is prone to get updated in a thread unsafe way??
If it is so, how do i make it thread safe, such that different requests to the servlets will use their own UIS's and not update the UIS of some other thread
Kindest regards
Saj
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
You shold be overiding doPost or Doget, not service. Yes, access to your vector is not thread safe. You can either Synchronize access to it, or implement the singleThread Model interface. Check out this link http://www.jguru.com/faq/view.jsp?EID=150 It should answer your questions.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
http://www.jguru.com/faq/view.jsp?EID=150


SCJO, SCJD, SCWCD, I-Net+, A+, Network+, MCSD, MCDBA, MCP, MCT
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
Why not just put your Vector in the HttpSession -- that way each User gets their own Vector.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Bosun Bello:
Yes, access to your vector is not thread safe.
Isn't it? I think it's a bit difficult to say without knowing what the Vector is for. If it is a global something that everyone just adds stuff into, it's fine (although it should probably be a ServletContext attribute in that case). Sure, multiple clients can add stuff at the same time, but the Vector is fine with that (it's synchronized) and if the application logic is fine with that too, the servlet is perfectly threadsafe as-is.
The last "if" is a biggie though. Reading between the lines, Saj may really want a vector per user session, or maybe per request, or even local to the servlet doGet() method.
- Peter

[This message has been edited by Peter den Haan (edited October 27, 2001).]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Servlets - help
 
Similar Threads
Servlet problem - help required
question regarding the sequence of events happening as given in head first jsp servlet
Custom Tags & Threading
put Vector in run(), and update him
Using synchronize on overloaded methods