1 package ch.hslu.exercises.sw06.ex4;
2
3 import org.slf4j.LoggerFactory;
4 import org.slf4j.Logger;
5
6
7
8
9 public final class Consumer implements Runnable {
10
11 private static final Logger LOG = LoggerFactory.getLogger(Consumer.class);
12 private final BoundedBuffer<Integer> queue;
13 private long sum;
14
15
16
17
18
19
20 public Consumer(final BoundedBuffer<Integer> queue) {
21 this.queue = queue;
22 this.sum = 0;
23 }
24
25 @Override
26 public void run() {
27 while (true) {
28 try {
29 Integer temp = queue.remove(10);
30 if (temp == null) {
31 LOG.info("Consumer get timeout");
32 break;
33 }
34 sum += temp;
35 } catch (InterruptedException ex) {
36 return;
37 }
38 }
39 }
40
41
42
43
44
45
46 public long getSum() {
47 return sum;
48 }
49 }