public class Counter { int c; static Object lock = new Object(); synchronized void increment() { this.c++; } public static void main (String arg[]) { Counter mycounter = new Counter(); /* thread 1 */ Thread t1 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 500; i++) { mycounter.increment(); System.out.println("t1:" + mycounter.c); if (mycounter.c == 250) { synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }); t1.start(); /* thread 2 */ Thread t2 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 500; i++) { mycounter.increment(); System.out.println("t2:" + mycounter.c); if (mycounter.c == 450) { synchronized (lock) { lock.notifyAll(); } } } } }); t2.start(); System.out.println("main:" + mycounter.c); } }