class Counter{
    private int c = 0;
    private int ic, dc;
    private void sleep(){
        try{ Thread.sleep(200); } catch(Exception e){ System.err.println(e); }
    }

    public void inc(){ ic++; sleep(); int newc = c + 1; sleep(); c = newc;}
    public void dec(){ dc++; sleep(); int newc = c - 1; sleep(); c = newc;}
    public void info(){ System.out.println(ic + " - " + dc + " = " + c); }
}

class IncCounterThread extends Thread{
    Counter c;
    IncCounterThread(Counter c){ this.c = c;} 
    public void run(){
        while(true){
	    synchronized(c){
		c.inc();
		c.info();
	    }
            try{
                Thread.sleep(100);
            }
            catch(Exception e){
            }
        }
    }
}

class DecCounterThread extends Thread{
    Counter c;
    DecCounterThread(Counter c){ this.c = c;} 
    public void run(){
        while(true){
	    //synchronized(c){
	    {
		c.dec();
		c.info();
	    }
            try{
                Thread.sleep(100);
            }
            catch(Exception e){
            }
        }
    }
}

public class CounterDemo{
    public static void main(String[] argv){
        Counter c = new Counter();
        IncCounterThread plus = new IncCounterThread(c);
        DecCounterThread minus = new DecCounterThread(c);

        plus.start();
        minus.start();
    }
}
        

