Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets - help

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jguru.com/faq/view.jsp?EID=150
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!