/** * 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"); }
/** Answers a question for a given employee */ private void answerQuestion() { // Gets the top question from the queue Employee employee; // If it's still before closing time if (office.getTime() < 1700) { // Notify that a question is being answered synchronized (questionLock) { employee = hasQuestion.poll(); questionLock.notifyAll(); } // Answer the question try { System.out.println( office.getStringTime() + " Manager starts answering question from Developer " + employee.getEmployeeName() + "."); sleep(100); System.out.println(office.getStringTime() + " Manager answers question."); } catch (InterruptedException e) { e.printStackTrace(); } // Tell the employee the answer to the question employee.questionAnswered(); // Notify remaining questioners synchronized (questionLock) { questionLock.notifyAll(); } } else { // If it's not closing time, keep answering questions while (!hasQuestion.isEmpty()) { employee = hasQuestion.poll(); employee.questionAnswered(); } // Inform employees that you have answered a question synchronized (questionLock) { questionLock.notifyAll(); } } }