public class MyThread3 { private int i; private synchronized void increase(){ i++; System.out.println(Thread.currentThread().getName()+"-increase:"+i); } private synchronized void decrease(){ i--; System.out.println(Thread.currentThread().getName()+"-decrease:"+i); } class Inc implements Runnable{ public void run(){ for(int j=0;i<100;j++){ increase(); } } } class Dec implements Runnable{ public void run(){ for(int j=0;j<100;j++){ decrease(); } } } public static void main(String args[]){ MyThread3 tt= new MyThread3(); Inc inc =tt.new Inc(); Dec dec = tt.new Dec(); for(int j=0;j<2;j++){ Thread t =new Thread(inc); t.start(); t=new Thread(dec); t.start(); } }}