Java: Thread safety in class with synchronized methods
<p>I read that the following class is not thread safe since threads could read inconsistent data as there is a chance for a thread to read scaled version of <code>real</code> and unscaled version of <code>imaginary</code>. But I did not understand how. </p>

<p>I was under the impression that if a thread acquires a lock and is in <code>scale()</code> method, no other thread can be in <code>getReal()</code> or <code>getImaginary()</code> methods at the same time so that other threads cannot read 'half scaled' complex numbers. Is it not correct?</p>

<pre><code>    class Complex 
    {   
         double real; 
         double imaginary;

         synchronized void scale(double scaleFactor)
         {
            real = real * scaleFactor;   
            imaginary = imaginary * scaleFactor; 
         }

         synchronized double getReal() 
         {
                return real;
         }

         synchronized double getImaginary() 
         {
               return imaginary;
         }
   }
</code></pre>
