crnixon (owner)

Revisions

  • 71ae5c Wed Sep 24 12:28:15 -0700 2008
gist: 12641 Download_button fork
public
Public Clone URL: git://gist.github.com/12641.git
Embed All Files: show embed
Java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class Floor {
  enum State { NO_ONE_WAITING, PASSENGERS_WAITING, LOADING }
  
  private int passengersWaiting = 0;
  private State state = State.NO_ONE_WAITING;
 
  synchronized public void waitForElevator(String msg) {
    if (state == State.LOADING) {
      try {
        wait();
      } catch (InterruptedException ex) {
      }
    }
 
    passengersWaiting++;
    System.out.println(msg);
 
    if (state == State.NO_ONE_WAITING) {
      this.state = State.PASSENGERS_WAITING;
      notifyAll();
    }
  }
 
  synchronized public void getOnOffElevator(String msg) {
    if (state != State.LOADING) {
      try {
        wait();
      } catch (InterruptedException ex) { }
    }
 
    passengersWaiting--;
    System.out.println(msg);
 
    if (passengersWaiting == 0) {
      this.state = State.NO_ONE_WAITING;
      notifyAll();
    }
  }
 
  synchronized public void elevatorArrives(String msg) {
    if (state == State.LOADING) {
      try {
        wait();
      } catch (InterruptedException ex) { }
    }
 
    System.out.println(msg);
 
    if (state == State.PASSENGERS_WAITING) {
      this.state = State.LOADING;
    } else {
      this.state = State.NO_ONE_WAITING;
    }
    notifyAll();
  }
 
  synchronized public void elevatorLeaves(String msg) {
    if (state != State.NO_ONE_WAITING) {
      try {
        wait();
      } catch (InterruptedException ex) { }
    }
 
    System.out.println(msg);
  }
}