TThread5.java
Select all
public class TThread5 { boolean done = false; private int count1 = 0, count2 = 0, count3 = 0; private void init() { class Adder extends Thread { public void run() { while (count1 <= 100) { count1++; count2++; count3++; try { Thread.sleep(50); } catch (InterruptedException e) { System.out.println("Application Interrupted"); } } done = true; } } class TestSync extends Thread { private int totAccess = 0; public void run() { int c1, c2, c3; while (!done) { totAccess++; c1 = count1; c2 = count2; c3 = count3; if ((c1 != c2) || (c2 != c3)) { System.out.println("\rNot synced: " + c1 + ", " + c2 + ", " + c3); } } System.out.println("Total sync accesses: " + totAccess); } } (new Adder()).start(); (new TestSync()).start(); } public static void main(String[] args) { TThread5 app = new TThread5(); app.init(); } }