Ejemplo n.º 1
0
  /**
   * @param order MessageOrder
   * @return boolean - true if a message expired and was processed
   */
  protected boolean processIncoming(MessageOrder order) {
    boolean result = false;
    Member member = order.getMessage().getAddress();
    Counter cnt = getInCounter(member);

    MessageOrder tmp = incoming.get(member);
    if (tmp != null) {
      order = MessageOrder.add(tmp, order);
    }

    while ((order != null) && (order.getMsgNr() <= cnt.getCounter())) {
      // we are right on target. process orders
      if (order.getMsgNr() == cnt.getCounter()) cnt.inc();
      else if (order.getMsgNr() > cnt.getCounter()) cnt.setCounter(order.getMsgNr());
      super.messageReceived(order.getMessage());
      order.setMessage(null);
      order = order.next;
    }
    MessageOrder head = order;
    MessageOrder prev = null;
    tmp = order;
    // flag to empty out the queue when it larger than maxQueue
    boolean empty = order != null ? order.getCount() >= maxQueue : false;
    while (tmp != null) {
      // process expired messages or empty out the queue
      if (tmp.isExpired(expire) || empty) {
        // reset the head
        if (tmp == head) head = tmp.next;
        cnt.setCounter(tmp.getMsgNr() + 1);
        if (getForwardExpired()) super.messageReceived(tmp.getMessage());
        tmp.setMessage(null);
        tmp = tmp.next;
        if (prev != null) prev.next = tmp;
        result = true;
      } else {
        prev = tmp;
        tmp = tmp.next;
      }
    }
    if (head == null) incoming.remove(member);
    else incoming.put(member, head);
    return result;
  }
Ejemplo n.º 2
0
 public static void main(String[] args) {
   System.out.println("This programs tests the features of the counter class.");
   Counter c1 = new Counter(), c2 = new Counter();
   tester(c1, c2);
   c2.decrement();
   tester(c1, c2);
   c1.setCounter(0);
   c2.increment();
   tester(c1, c2);
   c2.setCounter(c1.getCounter());
   tester(c1, c2);
   c1.decrement();
   tester(c1, c2);
   System.out.println("Test complete.");
 }
Ejemplo n.º 3
0
 public static void main(String[] args) throws InterruptedException {
   MyThread thread1 = new MyThread();
   MyThread thread2 = new MyThread();
   thread1.start();
   thread2.start();
   thread1.join();
   thread2.join();
   System.out.println(Counter.getCounter());
   System.out.println("Hellow to you too!");
   MyRunnable runnable = new MyRunnable(4, 2);
   Thread thread3 = new Thread(runnable);
   thread3.start();
   System.out.println("LOL I'm Not I'm a MAIN Thread!");
   ThreadVisibility thread4 = new ThreadVisibility();
   thread4.start();
   Thread.sleep(1000);
   thread4.setToFalse();
   System.out.println("keepRunning is false");
 }