• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listener Thread Question

 
Tom Scott
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't know - sounds a good question.

Would be interested to know the answer for filters too.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Scott
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion only you need is to synchronize these two methods. Thats all.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic