Threads and Synchronization in java
<p>Consider the below code for Thread Synchronization Method and a Synchronization Block</p>

<pre><code>public class ThreadSynchronizationPartI {
    public static int myValue=1;
    public static void main(String [] args)
    {
        Thread t=new Thread(()-&gt;
        {
            while(true)
            {
            updateBalance();
                }
        });
        t.start();

        t=new Thread(()-&gt;{
        while(true)
        {
            monitorBalance();
        }
        });
        t.start();
    }
    public static synchronized void updateBalance(){
        System.out.println("start "+myValue);
         myValue = myValue + 1;
        //    myValue = myValue - 1;
        System.out.println("end "+myValue);

    }
    public static synchronized void monitorBalance(){
        int b=myValue;
        if(b&gt;1)
        {
            System.out.println("B is greater than 1 by"+(b-1));
            System.exit(1);
        }
    }
}
</code></pre>

<p>Why Does it give the following output:
    start 1
    end 2
    start 2
    end 3
    start 3
    end 4
    start 4
    end 5
    start 5
    end 6
    start 6
    end 7
    start 7
    end 8
    B is greater than 1 by 7</p>

<p>Can anyone explain?</p>