jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes synchronization in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "synchronization in servlets" Watch "synchronization in servlets" New topic
Author

synchronization in servlets

vidhyasagar reddy
Ranch Hand

Joined: Jul 05, 2005
Posts: 40
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

Joined: Apr 08, 2005
Posts: 129
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.


"There are no mistakes, only lessons"
vidhyasagar reddy
Ranch Hand

Joined: Jul 05, 2005
Posts: 40
can u tell me under which conditions we have to use Vector not array list with some example code
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
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


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
sureshreddy puli
Greenhorn

Joined: Sep 28, 2005
Posts: 29
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronization in servlets