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
11
12 public SynchronizedCounter() {
13 this.count = 0;
14 }
15
16
17
18
19 @Override
20 public int increment() {
21 synchronized (lock) {
22 count++;
23 }
24 return count;
25 }
26
27
28
29
30 @Override
31 public int decrement() {
32 synchronized (lock) {
33 count--;
34 }
35 return count;
36 }
37
38
39
40
41 @Override
42 public int get() {
43 return count;
44 }
45 }