Пример #1
0
  /**
   * Asks a question for a given employee
   *
   * @param employee the Employee to assist
   */
  public void askQuestion(Employee employee) {
    // Add question to queue
    long startCheck = System.currentTimeMillis();

    synchronized (hasQuestion) {
      hasQuestion.add(employee);
    }

    // Waiting until question can be answered
    while (hasQuestion.contains(employee)) {
      // Is it time for the 4 o'clock meeting?
      try {
        if (office.getTime() >= 1600 && !employee.isAttendedEndOfDayMeeting()) {
          office.waitForEndOfDayMeeting();
          System.out.println(
              office.getStringTime()
                  + " Developer "
                  + employee.getEmployeeName()
                  + " attends end of day meeting");
          sleep(150);
          employee.setAttendedEndOfDayMeeting(true);
        }
        synchronized (questionLock) {
          // Tell the Manager there is a question.
          office.notifyWorking();
          questionLock.wait();
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    synchronized (questionLock) {
      // Question is being answered
      while (employee.isWaitingQuestion()) {
        try {
          questionLock.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }

    long endCheck = System.currentTimeMillis();

    employee.addToWaitingQuestion((endCheck - startCheck) / 10);

    System.out.println(
        office.getStringTime()
            + " Developer "
            + employee.getEmployeeName()
            + "'s question is answered");
  }
Пример #2
0
  public void checkConditions() {
    // If we haven't had our 10AM meeting but it's 10AM+...
    if (office.getTime() >= 1000 && !attendedMeeting1) {
      try {
        long startCheck = System.currentTimeMillis();
        // Go to the meeting
        System.out.println(office.getStringTime() + " Manager goes to meeting");
        sleep(600);
        long endCheck = System.currentTimeMillis();

        timeSpentInMeetings += (endCheck - startCheck) / 10;

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      // Now we have attended the first meeting
      attendedMeeting1 = true;
      // Notify people that we can answer questions
      synchronized (questionLock) {
        questionLock.notifyAll();
      }
    }
    // If it's around 12pm and we haven't eaten lunch yet
    if (office.getTime() >= 1200 && !ateLunch) {
      try {
        // Eat some lunch
        long startCheck = System.currentTimeMillis();
        System.out.println(office.getStringTime() + " Manager goes to lunch");
        sleep(600);
        long endCheck = System.currentTimeMillis();

        timeSpentAtLunch += (endCheck - startCheck) / 10;

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      // Now we have eaten lunch
      ateLunch = true;
      // Notify people that we can answer questions
      synchronized (questionLock) {
        questionLock.notifyAll();
      }
    }
    // If it's around 2PM and we haven't had the 2PM meeting yet
    if (office.getTime() >= 1400 && !attendedMeeting2) {
      try {
        // Go to the meeting
        long startCheck = System.currentTimeMillis();
        System.out.println(office.getStringTime() + " Manager goes to meeting");
        sleep(600);
        long endCheck = System.currentTimeMillis();

        timeSpentInMeetings += (endCheck - startCheck) / 10;

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      // Now we have attended the meeting
      attendedMeeting2 = true;
      // Notify people that we can answer questions
      synchronized (questionLock) {
        questionLock.notifyAll();
      }
    }

    // Is it time for the 4 o'clock meeting?
    if (office.getTime() >= 1600 && !attendedFinalMeeting) {
      // Announce to answer any immediate questions
      synchronized (questionLock) {
        questionLock.notifyAll();
      }
      // Notify the office leads about further questions
      for (int i = 0; i < 3; i++) {
        synchronized (office.getLead(i).getLeadQLock()) {
          office.getLead(i).getLeadQLock().notifyAll();
        }
      }
      // Wait for the end of the day meeting to start
      // System.out.println("Manager is waiting for end of day meeting");
      office.waitForEndOfDayMeeting();
      try {
        long startCheck = System.currentTimeMillis();
        System.out.println(office.getStringTime() + " Manager attends the end of day meeting");
        sleep(150);
        long endCheck = System.currentTimeMillis();

        timeSpentInMeetings += (endCheck - startCheck) / 10;

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      // Now we have attended the meeting
      attendedFinalMeeting = true;
      // Notify any remaining questioners
      synchronized (questionLock) {
        questionLock.notifyAll();
      }
    }
  }