• Post Reply Bookmark Topic Watch Topic
  • New Topic

using collections in session object  RSS feed

 
ravi v kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

currently, i am holding a treemap object and an arraylist object in session object. i made the collection as syncronizedList/Map using Collections in java.util

Is it good way to do it??

Is each session object use different treemap and arraylist or access the same collect across different session objects?


if i make my collection objects syncronized using Collections.syncronizedXXX(), can i modify the treemap and arraylist in session by opening the application in multiple windows using CRTL+N(iexplorer)
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are asking whether using Collections.synchronizeXXX() is the correct may of making collections thread-safe, then the answer depends on the concurrency requirements i.e. is the collection accessed too regularly by concurrent threads.

This boils down to saying how much is the contention for the lock that you use to synchronize. If you use Collections.synchronizeXXX() methods, all the methods are synchronized on the collection instance created by calling synchronizeXXX() method (and not the original collection). So, the contention will be huge if concurrent clients try to access the collection.

However, in your case since you are storing the collection in the session, concurrency will only come into picture when someone has lot of windows opened using CTRL+N and he simultaneously invokes the code that access the collections. This possibility is very remote and hence the lock will be sparingly contended. So, it seems reasonable to use Collections.synchronizeXXX() methods here.

However, if you asked whether storing these collections in session is correct or not, this depends on your application requirements.
[ January 22, 2008: Message edited by: Nitesh Kant ]
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ravi v kumar: Is each session object use different treemap and arraylist or access the same collect across different session objects?


* The same map/list is used by all threads.
* Simply creating a synchronized instance of a collection/map will not make the collection/map thread-safe. It has to be accessed only from synchronized blocks.

eg:

ravi v kumar: Is it good way to do it??


* java.util.concurrent.CopyOnWriteArrayList is an excellent alternate to synchronized arrayList. CopyOnWriteArrayList doesnt require access to be synchronized explicitly.i.e you dont have to follow the idiom specified above if CopyOnWriteArrayList is used. Concurrency is taken care internally.
* However,there is no concurrent equivalent for TreeMap. You will have to follow Collections.synchronizedXXX(..) way.

If there is any better way of achieving a concurrent treeMap,let me know.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarvesh: The same map/list is used by all threads.

Oops, i think i missed this point. Ravi, do you create these lists per user session or you share it across sessions? If you share a single copy of it, then you do not really have to put it in the session. Anyways, you know what the instance is, isnt? Also, in such a case the lock contention will be huge.
synchronizedXXX() methods create a wrapper over the original collection to make the access to the original collection synchronized. It does not clone/copy the original collection.

sarvesh: Simply creating a synchronized instance of a collection/map will not make the collection/map thread-safe. It has to be accessed only from synchronized blocks.

No only iteration has to be synchronized on the synhronized collection object. The reason being there is no way for the synchronized collection to give a thread-safe access during iteration i.e. ensure that the collection is not modified during ietration.

(Yanked from javadoc)
It is imperative that the user manually synchronize on the returned list when iterating over it


sarvesh: java.util.concurrent.CopyOnWriteArrayList is an excellent alternate to synchronized arrayList. CopyOnWriteArrayList doesnt require access to be synchronized explicitly.i.e you dont have to follow the idiom specified above if CopyOnWriteArrayList is used. Concurrency is taken care internally.

This is *only* recommended if the read operation totally out number write operations. Write operations are extremely costly on CopyOnWriteArrayList because it copies the entire list on write. So, beware!
ConcurrentLinkedQueue is infact a very superior implementation as it employs an efficient "wait-free" algorithm. See javadocs for details.

sarvesh: However,there is no concurrent equivalent for TreeMap.

True. Java 6 has ConcurrentSkipListMap that is the concurrent equivalent of TreeMap
[ January 23, 2008: Message edited by: Nitesh Kant ]
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Nitesh Kant : ConcurrentLinkedQueue is infact a very superior implementation as it employs an efficient "wait-free" algorithm


* True,but ConcurrentLinkedQueue is a queue. Replacing arrayList with a queue will affect the functionality of the application. So,one must stick to Collections.synchronizedList(..) or CopyOnWriteArrayList.


Nitesh Kant : Java 6 has ConcurrentSkipListMap that is the concurrent equivalent of TreeMap


* Thanks for the update !!
 
ravi v kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
thanks for the reply
application is like user select some of the records from the search and again he will start a fresh search with different criteria and add to the his selected list.

its like shopping cart.

i am getting request.getSession and adding these collections. so i think this collection will exist for that user only, session object take care i guess.

now the issue is with multiple same window with ctrl+n by same user.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravi: i am getting request.getSession and adding these collections. so i think this collection will exist for that user only, session object take care i guess.

Session does not take care of anything. It will return the objects that you have set. If the objects are shared across different users i.e. you do not do a new of the collections per user session then it will be shared by all the users.

ravi: now the issue is with multiple same window with ctrl+n by same user.

What is the issue here? Since you have synchronized the access, so you will not have data corruption of the collections.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!