• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets - help

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jguru.com/faq/view.jsp?EID=150
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic