• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why need to synchronize the context and session attributes ?

 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
->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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic