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 Producer implements Runnable {
10
11 private static final Logger LOG = LoggerFactory.getLogger(Producer.class);
12 private final BoundedBuffer<Integer> queue;
13 private final int maxRange;
14 private long sum;
15
16
17
18
19
20
21
22 public Producer(final BoundedBuffer<Integer> queue, final int max) {
23 this.queue = queue;
24 this.maxRange = max;
25 this.sum = 0;
26 }
27
28 @Override
29 public void run() {
30 for (int i = 0; i < maxRange; i++) {
31 try {
32 if (!queue.add(i, 10)) {
33 LOG.info("Producer put timeout");
34 break;
35 }
36 sum += i;
37 } catch (InterruptedException ex) {
38 return;
39 }
40 }
41 }
42
43
44
45
46
47
48 public long getSum() {
49 return sum;
50 }
51 }