M
MikL
I have an object that is accessed and updated by multiple threads, but the
application architecture ensures that only one thread at a time will do
this. As I understand it, I don't need to worry about using "synchronized"
to prevent concurrent access, but I _do_ need to use "synchronized" when a
new thread starts using it to ensure it doesn't use out-of-date cached
values on a multi-processor JVM.
I'm trying to avoid using synchronized methods:
1) to avoid the performance hit that I read about everywhere.
2) a multi-cycle "conversation" can occur between threads, so to avoid
deadlocks I don't want to synchronize at the API level.
3) a single API call to the class can internally call dozens of methods,
each of which accesses properties -- and then I'm faced with dozens of
synchronized blocks per API call.
Does this make sense? Is there a better way of doing things?
Here's a massively simplified illustration of what I'm thinking. The real
thing is far more complicated, with dozens of properties, and deeply nested
method calls.
public class MyClass
{
private int fThing;
// a background thread runs this
private void publishEvent (...)
{
SwingUtilities.invokeAndWait(new Runnable() { ...indirect callback to
setThing() }.)
synchronized (this) { /* ensure cache up to date after AWT thread
access finishes */ }
}
// this method is called back from the AWT-Event thread when triggered by
publishEvent()
public void setThing (int aValue)
{
synchonized(this) { /* ensure have up to date cache */ }
fThing = aValue;
// do more stuff
// possibly call publishEvent() recursively.
}
}
application architecture ensures that only one thread at a time will do
this. As I understand it, I don't need to worry about using "synchronized"
to prevent concurrent access, but I _do_ need to use "synchronized" when a
new thread starts using it to ensure it doesn't use out-of-date cached
values on a multi-processor JVM.
I'm trying to avoid using synchronized methods:
1) to avoid the performance hit that I read about everywhere.
2) a multi-cycle "conversation" can occur between threads, so to avoid
deadlocks I don't want to synchronize at the API level.
3) a single API call to the class can internally call dozens of methods,
each of which accesses properties -- and then I'm faced with dozens of
synchronized blocks per API call.
Does this make sense? Is there a better way of doing things?
Here's a massively simplified illustration of what I'm thinking. The real
thing is far more complicated, with dozens of properties, and deeply nested
method calls.
public class MyClass
{
private int fThing;
// a background thread runs this
private void publishEvent (...)
{
SwingUtilities.invokeAndWait(new Runnable() { ...indirect callback to
setThing() }.)
synchronized (this) { /* ensure cache up to date after AWT thread
access finishes */ }
}
// this method is called back from the AWT-Event thread when triggered by
publishEvent()
public void setThing (int aValue)
{
synchonized(this) { /* ensure have up to date cache */ }
fThing = aValue;
// do more stuff
// possibly call publishEvent() recursively.
}
}