wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Listener Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Listener Thread Question" Watch "Listener Thread Question" New topic
Author

Listener Thread Question

Tom Scott
Greenhorn

Joined: Aug 18, 2006
Posts: 21
Hi,

Are there any threading issues with Listener class instance variables? Specifically if I try to implement a Listener something like this...

Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
I really don't know - sounds a good question.

Would be interested to know the answer for filters too.


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
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.


Ping & DNS - my free Android networking tools app
Tom Scott
Greenhorn

Joined: Aug 18, 2006
Posts: 21
Hi Ulf,

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
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: 41150
    
  45
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
In my opinion only you need is to synchronize these two methods. Thats all.


<a href="http://www.dantheman.pl" target="_blank" rel="nofollow">http://www.dantheman.pl</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listener Thread Question
 
Similar Threads
Not recieving sessionDestroyed on Browser close
sessionDestroyed works, but not sessionCreated
Invalidate Question
HttpSessionListener
Q. Implementation of interface HttpSessionListener