There's only one SessionListener object which gets called for all sessions. So, yes, since sessions can be created in parallel threads, the listener object can be called from parallel threads, and it needs to be programmed in a thread-safe manner.
Many thanks for the reply. I suspect my question isn't too relevent to the exam since I've not had many answers but I would appreciate any further confirmation or maybe a suggestion of how such functionality would usually be achieved. I wondered about synchronizing on the Listener class object...but then I wondered if this would affect performance and if maybe I should create an object specificaly to guard updates to the count...
Thanks in advance..
Joined: Aug 18, 2006
And then I thought all calls to the Listener affect the count. So either we synchronize or we don't - but I'm unclear if we need to or not!
Joined: Mar 22, 2005
Well, as I said, the code needs to be written in a multithread-safe way. That means that operations like "++sessionCount" need to be synchronized to make them atomic. Using the object itself for synchronizing should be no big performance problem, unless you have other -more time-consuming- blocks of code that also need protection.
Joined: Nov 06, 2006
In my opinion only you need is to synchronize these two methods. Thats all.
Originally posted by Dan Polak: In my opinion only you need is to synchronize these two methods.
That is sufficient, but not necessary. The real class will have more elaborate method bodies, and synchronizing the complete method may become a performance bottleneck (if there are many session being created or removed), since only access to the sessionCount field needs to be synchronized.