This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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..
-Tom
Tom Scott
Greenhorn
Joined: Aug 18, 2006
Posts: 21
posted
0
Hmm,
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!
-Tom
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
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.
Dan Polak
Ranch Hand
Joined: Nov 06, 2006
Posts: 32
posted
0
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.