public class Main { class Tq { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } } public static void main(String[] args) { Tq c = new Main().new Tq(); c.value(); Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println("thread1"); for (int i = 0; i < 1000000 ; i++) { c.increment(); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 1000000 ; i++) { c.decrement(); } System.out.println("thread2"); } }); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch ( Exception e ) { System.out.println("no"); } System.out.println( c.value() ); }