package garden;

public class SimpleCounter implements Counter {

	private int count_;

	public SimpleCounter () {
		count_ = 0;
	}

	/*
	 * (non-Javadoc)
	 * @see Counter#getValue()
	 */
	@Override
	public int getValue () {
		return count_;
	}

	/*
	 * (non-Javadoc)
	 * @see Counter#increment()
	 */
	@Override
	public void increment () {
		// one would normally just write
		// count_++
		// but that isn't atomic either
		int value = count_;
		Thread.yield(); // give another thread a chance
		count_ = value + 1;
	}

	public void decrement () {
		int value = count_;
		Thread.yield(); // give another thread a chance
		count_ = value - 1;
	}
}
