• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[Solved]Inheriting Global variables

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a superclass A which has a TreeMap needed to be accessed by A subclass when an event it fired (an unsubscribe notification), there are 4 objects of class B instantiated. However I get ConcurrentModificationException when all the classes access it, Im guessing this is because they are all trying to access the same A.x TreeMap.

Each subclass of A should have its own local copy of the TreeMap, is there anyway to declare this in A or do all subclasses have to declare it so there is no ConcurrentModificationException.

I hope that makes sense, thanks for any help
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually it does not make sense to me,

I dont think you should face a ConcurrentModificationException if there are seperate Objects...

- are you using the same Object ?
- are you using seperate Objects but a static variable containing the collection ?


I would normally recommend to use a synchronized block, but I want to understand your code first....

Please provide a code snippet to help us help you better...
 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TreeMap is not a static variable.

Class A has



and Class B:



Theres more code than that but that is all the is resulting in throwing the exception, its not threaded so they aren't accessing the same variable in different threads. The only way I can see it happening is through inheritance.

Thanks
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that the code that calls somefunction is passing in x.

Is the body of the loop doing anything to modify the TreeMap (or the pubKeys set)? The forEach loop uses the set's iterator, which will throw an exception on the NEXT use of the iterator after a change to the underlying collection, if the change is not made through the iterator - from TreeMap javadoc:

if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.



Since you are using a forEach loop, you don't actually have access to the iterator it is using, so any changes you make to the collection will by definition not be through the iterator's remove method. Since it is the iterator that throws the exception, it will not be thrown until the next use of it, which is why the exception occurs on the forEach line, rather than within the body of the loop.
 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks thats sorted it, rather than the forEach loop, I sent it to an Array and used that.

Thanks for all your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic