Simplified Synchronization Examples To more simply illustrate the sharing of data by threads, I revise the increment method to of TallyWrong.java and TallyRight.java to save the number in the Buffer in a local variable i. Each method then does some computation, here simulated by a loop that repeats a multiplication. At the end of the computation, we increment the variable i, simulating a change due to the computation, and save it in the number field. In TallyWrong.java a thread may lose its turn after it has obtained a value for i, but before it has saved the updated value. The second thread will access the same number field and change its value, but when the first thread returns it will overwrite the number field with the old saved value in the variable i. TallyRight.java makes the increment method synchronized so that each thread will finish the increment method before losing its turn. TallyWrong.java /* Two threads occasionally err in reporting * values because they get interrupted before * finishing to execute a method. */ public class TallyWrong { class Buffer { int number = 0; // the number to increase or decrease int total = 0; // total number of increments public void increment() { int i = number; total++; for(int j = 0; j < 50000; j++) { int k = j*j; } number = ++i; } public String result() { return "total = " + total + " number = " + number; } } class Plus extends Thread { Buffer buf; Plus(Buffer b) { buf=b; } public void run() { for(int i = 0; i < 100; i++){ System.out.println(Thread.currentThread().getName() + " " + buf.result()); buf.increment(); } System.out.println(buf.result()); } } public static void main(String[] argv) { TallyWrong tw = new TallyWrong(); Buffer b = tw.new Buffer(); Plus p1 = tw.new Plus(b); Plus p2 = tw.new Plus(b); p1.start(); p2.start(); } } Output (Showing only significant lines) Thread-0 total = 0 number = 0 ... Thread-0 total = 45 number = 45 Thread-1 total = 46 number = 45 ... Thread-1 total = 130 number = 129 Thread-0 total = 131 number = 46 ... Thread-0 total = 184 number = 99 total = 185 number = 100 Thread-1 total = 185 number = 130 ... Thread-1 total = 199 number = 144 total = 200 number = 145 TallyRight.java /* Uses synchronized methods to correct the error. */ public class TallyRight { class Buffer { int number = 0; // the number to increase or decrease int total = 0; // total number of increments public synchronized void increment() { int i = number; total++; for(int j = 0; j < 50000; j++) { int k = j*j; } number = ++i; } public synchronized String result() { return "total = " + total + " number = " + number; } } class Plus extends Thread { Buffer buf; Plus(Buffer b) { buf=b; } public void run() { for(int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + " " + buf.result()); buf.increment(); } System.out.println(buf.result()); } } public static void main(String[] argv) { TallyRight tr = new TallyRight(); Buffer b = tr.new Buffer(); Plus p1 = tr.new Plus(b); Plus p2 = tr.new Plus(b); p1.start(); p2.start(); } } Output (Showing only significant lines) Thread-0 total = 0 number = 0 ... Thread-0 total = 45 number = 45 Thread-1 total = 46 number = 46 ... Thread-1 total = 145 number = 145 total = 146 number = 146 Thread-0 total = 146 number = 146 ... Thread-0 total = 199 number = 199 total = 200 number = 200