public class Thread3 implements Runnable { Integer i = 1; public Thread3() { } public void run() { String threadName = Thread.currentThread().getName(); if (threadName.equals("a")) { while (true) { inc(); } } else while (true) { dec(); } } public synchronized void inc() { i++; System.out.println("inc " + i); this.notify(); try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public synchronized void dec() { // TODO Auto-generated catch block i--; System.out.println("dec " + i); this.notify(); try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // i.notify(); } ; /** * @param args */ public static void main(String[] args) { Thread3 th3 = new Thread3(); Thread t = new Thread(th3, "a"); t.start(); new Thread(th3, "b").start(); } }