1 package ch.hslu.exercises.sw06.ex2;
2
3 import java.util.Random;
4 import java.util.concurrent.Semaphore;
5
6 import org.slf4j.LoggerFactory;
7 import org.slf4j.Logger;
8
9
10
11
12
13 public final class RaceHorse implements Runnable {
14
15 private static final Logger LOG = LoggerFactory.getLogger(RaceHorse.class);
16 private final Synch startSignal;
17 private final Semaphore counter;
18 private final String name;
19 private final Random random;
20
21
22
23
24
25
26
27 public RaceHorse(final Synch startSignal, final String name, final Semaphore counter) {
28 this.startSignal = startSignal;
29 this.name = name;
30 this.random = new Random();
31 this.counter = counter;
32 }
33
34 @Override
35 public void run() {
36 LOG.info("Rennpferd {} geht in die Box.", name);
37 try {
38 counter.release();
39 startSignal.acquire();
40 LOG.info("Rennpferd {} laeuft los...", name);
41 Thread.sleep(random.nextInt(3000));
42 } catch (InterruptedException ex) {
43 LOG.debug("Ich muss nicht mehr ins Ziel laufen");
44 }
45 LOG.info("Rennpferd {} ist im Ziel.", name);
46 }
47 }