• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Listener Thread Question

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't know - sounds a good question.

Would be interested to know the answer for filters too.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion only you need is to synchronize these two methods. Thats all.
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic