コード例 #1
0
  public static void main(String[] args) {
    IDoor door = new SingleDoor();
    JFrame j = new JFrame();
    j.add(door.createDoorUI());
    j.setVisible(true);

    synchronized (door) {
      door.openDoor();
      try {
        door.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
コード例 #2
0
  @Override
  public void run() {

    int i = 0;
    if (command.equalsIgnoreCase("Open")) {
      synchronized (door) {
        while (i < 5) {
          System.out.println("i value" + i);
          door.setDoorStatus("OPENING");
          try {
            Thread.currentThread().sleep(500); // 2000
          } catch (InterruptedException e) {
            return; // e.printStackTrace();
          }
          i = i + 2;
        }

        door.setDoorStatus("OPENED");
        door.notifyAll();
      }
    } else {

      synchronized (door) {
        while (i < 5) {
          System.out.println("i value" + i);
          door.setDoorStatus("CLOSING");
          try {
            Thread.currentThread().sleep(500); // 2000
          } catch (InterruptedException e) {
            return; // e.printStackTrace();
          }
          i = i + 2;
        }

        door.setDoorStatus("CLOSED");
        door.notifyAll();
      }
    }
  }