• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronizing base class members  RSS feed

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags. It's hard to read such a long listing...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!