Пример #1
0
 /** 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();
     }
   }
 }
 public void doFunctionalQueryTest(IMap imap) {
   Employee em = new Employee("joe", 33, false, 14.56);
   imap.put("1", new Employee("joe", 33, false, 14.56));
   imap.put("2", new Employee("ali", 23, true, 15.00));
   for (int i = 3; i < 103; i++) {
     imap.put(
         String.valueOf(i), new Employee("name" + i, i % 60, ((i % 2) == 1), Double.valueOf(i)));
   }
   Set<Map.Entry> entries = imap.entrySet();
   assertEquals(102, entries.size());
   int itCount = 0;
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     itCount++;
   }
   assertEquals(102, itCount);
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.is("active").and(e.get("age").equal(23));
   entries = imap.entrySet(predicate);
   assertEquals(3, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
   imap.remove("2");
   entries = imap.entrySet(predicate);
   assertEquals(2, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
 }
 @Test
 public void putAndGetEmployeeObjects() {
   HazelcastClient hClient = getHazelcastClient();
   int counter = 1000;
   Map<String, Employee> clientMap = hClient.getMap("putAndGetEmployeeObjects");
   for (int i = 0; i < counter; i++) {
     Employee employee = new Employee("name" + i, i, true, 5000 + i);
     employee.setMiddleName("middle" + i);
     employee.setFamilyName("familiy" + i);
     clientMap.put("" + i, employee);
   }
   for (int i = 0; i < counter; i++) {
     Employee e = clientMap.get("" + i);
     assertEquals("name" + i, e.getName());
     assertEquals("middle" + i, e.getMiddleName());
     assertEquals("familiy" + i, e.getFamilyName());
     assertEquals(i, e.getAge());
     assertEquals(true, e.isActive());
     assertEquals(5000 + i, e.getSalary(), 0);
   }
   //        }
 }
Пример #4
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");
  }