title: Java Thread Safety: How to handle an instance variable when you have to call methods on it
body: <p>So I'm pretty good overall when it comes to the thread-safety of code, but I'm currently encountering a situation where I'm not clear on the best way to handle it.</p>
  
  <p>I have an instance variable, non-final because the enclosing class can change its value. This variable is an object that has several methods that get called on it during the application. Here is a quick sample to show what I mean.</p>
  
  <pre><code>private class Foo{
  
          private FooFoo fooFoo;
  
          public synchronized void setFooFoo(FooFoo fooFoo){
              this.fooFoo = fooFoo;
          }
  
          public void doSomething(){
              fooFoo.doSomething(); //How do I make this line thread-safe?
          }
  
      }
  </code></pre>
  
  <p>Changing the reference of the fooFoo field is easy, just simple synchronization. But what about when the doSomething() method is called on fooFoo? I'm always hesitant to synchronize on an alien method due to deadlock risk.</p>
  
  <p>In the real cases this is based on, there are many different variations of this. My company's application is a sprawling code base that frequently resembles a bowl of spaghetti, so when it comes to writing any kind of synchronized code I'm extra paranoid, because of the tight coupling and the fact that there are developers not only here in the US, but in an offshore firm in eastern europe working on it and I do not trust all of them to make good coding decisions.</p>
  
  <p>So I'm just looking for the best practice to handle this kind of situation in a multi-threaded environment. Thanks.</p>