• 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

synchronizing base class members

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a very simple hierarchy laid out here.

Base class:
========

class A{

protected ObjectX m_data;
public Object getAttribute(String name)
{
if(m_data!=null){.......}
}

}


Inheriting classes:
============

public class B extends A {

protected B(ObjectX dataValues)
{
m_data = dataValues;
}


public String getName()
{
if(m_data==null){RefreshData();}
String name=m_data.getAttribute("NAME");
return name;
}

public boolean RefreshData()
{ ....;
.....;
m_data=xxx;
}

}

public class C extends A {

protected C(ObjectX dataValues)
{
m_data = dataValues;
}

public String getAddress(){
if(m_data==null){RefreshData();}
String address=m_data.getAttribute("ADDRESS");
return address;
}

public boolean RefreshData(){
. ...;
.....;
m_data=xxx;
}

}

Important points:
1.A singular instance of B and C are stored in a static hashmap against their respective keys.
So at any instance,we would have multiple clients accessing the same reference of B(and C),and therefore potentially m_data too.
2.The algorithm to refresh m_data is different for inheriting classes B and C - so ' RefreshData()' cannot be promoted to base.

Please suggest the most apt synchronization technique for m_data.
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A basic follow-up question question :

class A{

private ObjectX data;
public synchronized Object getData(){return data;}
public synchronize void setData(ObjectX p){data=p;}
public String getAddress(){ String xx;
synchronized(this){
xx=data.getAddress();
}
return xx;
}
Do these locks work on the same instance data i.e. synchronized and synchronized(this) work on the same lock?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags. It's hard to read such a long listing...
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic