• 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

synchronization in servlets

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u tell me under which conditions we have to use Vector not array list with some example code
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic