View Javadoc
1   package ch.hslu.exercises.sw08.ex2;
2   
3   public final class SynchronizedCounter implements Counter {
4   
5       private int count;
6   
7       private final Object lock = new Object();
8   
9       /**
10       * Erzeugt einen Zähler mit Zählerstand 0.
11       */
12      public SynchronizedCounter() {
13          this.count = 0;
14      }
15  
16      /**
17       * see ch.hslu.ad.exercise.sw07.count.Counter#increment()
18       */
19      @Override
20      public int increment() {
21          synchronized (lock) {
22              count++;
23          }
24          return count;
25      }
26  
27      /**
28       * see ch.hslu.ad.exercise.sw07.count.Counter#decrement()
29       */
30      @Override
31      public int decrement() {
32          synchronized (lock) {
33              count--;
34          }
35          return count;
36      }
37  
38      /**
39       * see ch.hslu.ad.exercise.sw07.count.Counter#get()
40       */
41      @Override
42      public int get() {
43          return count;
44      }
45  }