SynchronizedCounter.java

package ch.hslu.exercises.sw08.ex2;

public final class SynchronizedCounter implements Counter {

    private int count;

    private final Object lock = new Object();

    /**
     * Erzeugt einen Zähler mit Zählerstand 0.
     */
    public SynchronizedCounter() {
        this.count = 0;
    }

    /**
     * see ch.hslu.ad.exercise.sw07.count.Counter#increment()
     */
    @Override
    public int increment() {
        synchronized (lock) {
            count++;
        }
        return count;
    }

    /**
     * see ch.hslu.ad.exercise.sw07.count.Counter#decrement()
     */
    @Override
    public int decrement() {
        synchronized (lock) {
            count--;
        }
        return count;
    }

    /**
     * see ch.hslu.ad.exercise.sw07.count.Counter#get()
     */
    @Override
    public int get() {
        return count;
    }
}