This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization in servlets

 
vidhyasagar reddy
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i was using the arraylist in the place of vector in a servlet
does it make any difference because vector is synchronized and arraylist is not synchronized right !
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you dont have any specific requirment of synchronisation , you shuld use ArrayList.It will improve the speed because vector takes time in synchronisation of its elements which is not the case with ArrayList.
 
vidhyasagar reddy
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u tell me under which conditions we have to use Vector not array list with some example code
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Reddy,

This question that you have asked is more generic. There are n reasons for when to go for Vector and when for array list. But, it all depends on the application.

Servlet runs on server side. It is instantiated once (but depending on webserver, there can be more than 1 instance for load balancing) and whenever a user request for its service, ONE NEW THREAD for this servlet is created for you.

Thus the entire service method should be synchronized. I mean it should handle concurrent access of the member variables that your servlet possess. So, if you have a ArrayList in your servlet, you will have to take care of its synchronization.

When multiple threads of the same servlet access this ArrayList, it should lead to consistent data read/store and prevent dirty reads.

Hope you got me. Vector might be good I believe.

All those experts there ... correct me If I am wrong!

Thanks!
Mani
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vidhyasagar reddy:
can u tell me under which conditions we have to use Vector not array list with some example code



If u r adding instance variables to arraylist there may be some sharing violations i.e when two users are accessing same object.So, try to use synchronized blocks while accessing instance variables stored in arraylist.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic