public class UseCounter8 {
	Counter c;                         
	public UseCounter8() {
		c = new Counter(0);
		for (int i = 0; i < 10; i++) {
      final int userID = i;
		  new Thread() {
		    public void run() { callCounter(userID); }
		  }.start();
		}
	}

	private void callCounter(int userID) {
    int val = 0;
    try {
      Thread.sleep(20);
		  val = c.getAndIncrement(); 
    } catch (InterruptedException e) {}
		System.out.println("User " + userID + " reporting. Value of counter = " + val);
	}
	
	public static void main(String[] args) {
		new UseCounter8();
	}
}

class Counter {
	private int value;

	public Counter(int c) {
		value = c;
	}

	public int getValue() {
		return value;
	}

	public synchronized int getAndIncrement() {
		int temp = 0;
		temp = value;
		value = temp + 1;
		return temp;
	}
}
