aspose file tools*
The moose likes Servlets and the fly likes  why need to synchronize the context and session attributes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " why need to synchronize the context and session attributes ? " Watch " why need to synchronize the context and session attributes ? " New topic
Author

why need to synchronize the context and session attributes ?

munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

->when we synchronize the context or session attributes , the only one thread can access it...
so it means that we are indirectly making the single thread model,
some body told me to not use single thread model.somebody please explain
the disadvantage of single thread model also...

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Synchronizing session, context objects means making any shared and modifiable data within them thread safe. That should be done very carefully to minimize the overhead. But the other methods (assuming synchronizing on a block inside the method) of the servlet instance is accessible to other threads. On the other hand the single thread model make the entire servlet instance dedicated to only one thread at a time. But even with that it's not possible to protect static members of the servlet instance or any other uses of context, session attributes without explicit synchronization. That's why it's deprecated as it doesn't give any good for the developer.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
synchronize the context or session attributes


That statement shows how using incorrect language leads to errors.

If synchronization is necessary, you synchronize only on the access to attributes, as soon as the retrieval or modification is done, which should take very few CPU cycles, synchronization is dropped.

Bill
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

synchronization is very costly and affect the performance so make sure what you need to synchronize and as said minimize it as much as possible.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Javin Paul wrote:synchronization is very costly and affect the performance so make sure what you need to synchronize and as said minimize it as much as possible.


I don't think synchronization by itself is very costly in never versions of the JDK/JRE. However it does restrict what can process in parallel and so yes you do need to make sure synchronization is minimal as possible.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
wood burning stoves
 
subject: why need to synchronize the context and session attributes ?