public class Wacky1 extends Thread { // Correct Version static private int n=0; // shared counter private int count; // how long to run; public Wacky1(int i) { count = i; } static synchronized public void inc() { int j = n+1; try { sleep((int)(Math.random() * 10)); } catch (InterruptedException e) {} n = j; try { sleep((int)(Math.random() * 10)); } catch (InterruptedException e) {} } public static void main(String[] args) { Thread t1 = new Wacky1(100); Thread t2 = new Wacky1(100); Thread t3 = new Wacky1(100); t1.start(); t2.start(); t3.start(); try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException e) {} System.out.println("Final value " + n); } public void run() { for (int i = 0; i < count; i++) { inc(); } } }