So I have the following scenario (can't share the actual code, but it would be something like this):
public class Test
{
private Object obj;
public void init()
{
service.registerListener(new InnerTest());
}
public void readObj()
{
// read obj here
}
private class InnerTest implements Listener
{
public synchronized void updateObj()
{
Test.this.obj = new Object();
// change the obj
}
}
}
The InnerTest
class is registered as listener in a service. That Service is running in one thread the calls to readObj()
are made from a different thread, hence my question, to ensure consistency of the obj
is it enough to make the UpdateObj()
method synchronized?