Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Is HFS incorrect about this?  RSS feed

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Head First Servlet P199's dialog on the thread-safety issues. The last comment says 'must synchronize on HttpSession'. I think this is not necessary. 2 threads can use any arbitrary objects to synchronize the access to shared resources. Am I right?
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't have the book with me but this is my guess. Yes you are technically correct, but if you think about it, I think the authors intention is more that the easiest way is to synchronise on the HttpSession object otherwise you will need to come up with a common (comm across threads that is) object that you can synchronise on. This is not always an easy task.

Mat
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello


2 threads can use any arbitrary objects to synchronize the access to shared resources.



Can u explain what you mean by this?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use any arbitrary object as lock to control the access to the shared resource wrapped by a java object and stored in the HttpSession object,then when some thread comes into the synchronized block and use this object,in this case you can't ensure that the object isn't changed by another thread,because this object can be used in other places.as a example, user open two windows and the will visit two threads,one changed the value of the object, and the other cannot be informed.so the lock has to be the HttpSesion object.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you're wrong. You must synchronize on HttpSession. That's because it's the only object that you really have bound to a client and accessible throughout various requests (this is called scope). Any other object you would have to use the HttpSession to get it, and then synchronize on it, but think about it - you already are accessing the HttpSession, unsynchronized.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!