 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
km Guest
|
Posted: Fri Jul 15, 2005 3:05 am Post subject: Synchronization on member objects of a class |
|
|
I am working on a multithread program that keeps updating an data
object at real time, while periodically query the object through a
reference in a separate class. It looks like the following:
public class Updater{
public HashMap data;
public Updater(Map _data){...};
public updateData()
{
//updates HashMap data. Probably insertion or deletion
}
}
public class Query{
public Updater a;
public Query(Updater _a){ a=_a; ... }
public void sendQuery()
{
//work on a, probably iteration through a...
}
}
What's the right procedure to synchronize these objects? Should I use
something like synchronized(data) in the Updater class's updateData
function and syncharize(a.data) block in Query? Will these blocks be
controlled by the same lock? If I have other code that may access the
data object in Updater do I have to synchronize them too? (If don't,
what'll happen?)
Thanks a lot.
-km-
|
|
| Back to top |
|
 |
Hemal Pandya Guest
|
Posted: Fri Jul 15, 2005 4:31 am Post subject: Re: Synchronization on member objects of a class |
|
|
km wrote:
| Quote: | I am working on a multithread program that keeps updating an data
object at real time, while periodically query the object through a
reference in a separate class. It looks like the following:
public class Updater{
public HashMap data;
public Updater(Map _data){...};
public updateData()
{
//updates HashMap data. Probably insertion or deletion
}
}
public class Query{
public Updater a;
public Query(Updater _a){ a=_a; ... }
public void sendQuery()
{
//work on a, probably iteration through a...
}
}
What's the right procedure to synchronize these objects? Should I use
something like synchronized(data) in the Updater class's updateData
function
|
That would work. Or you could just mark entire updateData synchronized,
thereby synchronizing on the Update object itself.
| Quote: | and syncharize(a.data) block in Query?
|
If you go this route that Updater.data will have to be visible to Query
and therefore to some other classes as well. That might be dangerous.
At any rate, the Query will have to be synchronized on the same object
that updateData is synchronized on.
| Quote: | Will these blocks be
controlled by the same lock? If I have other code that may access the
data object in Updater do I have to synchronize them too? (If don't,
what'll happen?)
|
They will be able to access Updater.data in parallel. Which might be
dangerous if they modify Updater.data.
|
|
| Back to top |
|
 |
km Guest
|
Posted: Fri Jul 15, 2005 5:22 am Post subject: Re: Synchronization on member objects of a class |
|
|
Hemal Pandya wrote:
| Quote: | and syncharize(a.data) block in Query?
If you go this route that Updater.data will have to be visible to Query
and therefore to some other classes as well. That might be dangerous.
At any rate, the Query will have to be synchronized on the same object
that updateData is synchronized on.
|
Thanks for comments. The Query class will only iterate through the map.
However, it may be incremented by Updater in the middle of the
iteration and as I understand it, the iterator will fail-fast, and
generate a ConcurrentModificationException. What other options do I
have to avoid that?
-km-
|
|
| Back to top |
|
 |
Hemal Pandya Guest
|
Posted: Fri Jul 15, 2005 8:14 am Post subject: Re: Synchronization on member objects of a class |
|
|
| Quote: | Hemal Pandya wrote:
and syncharize(a.data) block in Query?
If you go this route that Updater.data will have to be visible to Query
and therefore to some other classes as well. That might be dangerous.
At any rate, the Query will have to be synchronized on the same object
that updateData is synchronized on.
Thanks for comments. The Query class will only iterate through the map.
However, it may be incremented by Updater in the middle of the
iteration and as I understand it, the iterator will fail-fast, and
generate a ConcurrentModificationException. What other options do I
have to avoid that?
|
How do you restrict access to the data object to only the Updater and
Query objects? Just to clarify, this is no longer synchronization
issue.
One approach is to centralize the creation of Data, Query and Updater
objects. The Manager creates a Data object and instantiates Updater and
Query passing the Data as parameter, which each of them store as
private members and access only in blocks synchronized on Data.
class Manager {
void initialize() {
Data d = new Data();
q = new Query(d);
u = new Updater(d);
}
class Updater{
Data d;
Updater{Data d) { this.d = d; }
doUpdate() { synchronized (d) { ..... } }
}
class Query{.....}
You get the point. After writing this I realize this is not very
different from your original example at all. Except that Query has a
handle to Data instead of Updater. But I atleast find that a crucial
difference.
hth
(Editing disabled while spellchecking)
Stop spell checking
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|