public class Intrinsiclocks { public static int counter = 0; public static void increment() { int temp = counter; try { Thread.sleep(Math.round((Math.random() * 10))); } catch (InterruptedException e) { e.printStackTrace(); } temp++; counter = temp; } public static void main(String[] args) { Thread T1 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) increment(); } }); Thread T2 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) increment(); } }); T1.start(); T2.start(); try { T1.join(); T2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("The counter is : " + counter); } /* public static int counter1 = 0; public static int counter2 = 0; public static synchronized void increment1() { counter1++; } public static synchronized void increment2() { counter2++; } public static void increment1() { int temp = counter1; try { Thread.sleep(Math.round((Math.random() * 10))); } catch (InterruptedException e) { e.printStackTrace(); } temp++; counter1 = temp; } public static void increment2() { int temp = counter1; try { Thread.sleep(Math.round((Math.random() * 10))); } catch (InterruptedException e) { e.printStackTrace(); } temp++; counter1 = temp; } */ }