예제 #1
0
  /** This is the heart of the event manager, which processes the events for a robot. */
  public void processEvents() {
    // Remove old events
    eventQueue.clear(getTime() - MAX_EVENT_STACK);

    // Process custom events
    for (Condition customEvent : customEvents) {
      boolean conditionSatisfied = callUserCode(customEvent);
      if (conditionSatisfied) {
        addImpl(new CustomEvent(customEvent));
      }
    }

    // Sort the events based on the time and priority of the events
    eventQueue.sort();

    // Process event queue here
    Event currentEvent;
    while ((currentEvent = (eventQueue.size() > 0) ? eventQueue.get(0) : null) != null
        && currentEvent.getPriority() >= currentTopEventPriority) {

      if (currentEvent.getPriority() == currentTopEventPriority) {
        if (currentTopEventPriority > Integer.MIN_VALUE
            && isInterruptible(currentTopEventPriority)) {
          setInterruptible(currentTopEventPriority, false); // we're going to restart it, so reset.

          // We are already in an event handler, took action, and a new event was generated.
          // So we want to break out of the old handler to process it here.
          throw new EventInterruptedException(currentEvent.getPriority());
        }
        break;
      }

      int oldTopEventPriority = currentTopEventPriority;

      currentTopEventPriority = currentEvent.getPriority();
      currentTopEvent = currentEvent;

      eventQueue.remove(currentEvent);
      try {
        dispatch(currentEvent);

        setInterruptible(currentTopEventPriority, false);

      } catch (EventInterruptedException e) {
        currentTopEvent = null;
      } catch (RuntimeException e) {
        currentTopEvent = null;
        throw e;
      } catch (Error e) {
        currentTopEvent = null;
        throw e;
      } finally {
        currentTopEventPriority = oldTopEventPriority;
      }
    }
  }
 @Override
 public int compareTo(Event e) {
   if (priority > e.getPriority()) {
     return -1;
   } else if (priority < e.getPriority()) {
     return 1;
   } else {
     return 0;
   }
 }
예제 #3
0
 /**
  * Returns the priority of events belonging to a specific class.
  *
  * @param eventClass is a string with the full class name of the event type to get the priority
  *     from.
  * @return the event priority of the specified event class.
  * @see robocode.Event#getPriority()
  */
 public int getEventPriority(String eventClass) {
   if (eventClass == null) {
     return -1;
   }
   final Event event = eventNames.get(eventClass);
   if (event == null) {
     return -1;
   }
   return event.getPriority();
 }
  public static void main(String[] args) {

    PriorityBlockingQueue<Event> queue = new PriorityBlockingQueue<Event>();

    Thread[] taskThreads = new Thread[5];

    // 初始这 5 个任务线程。
    for (int i = 0; i < taskThreads.length; i++) {
      Task task = new Task(i, queue);
      taskThreads[i] = new Thread(task);
    }

    // 启动这 5 个任务线程。
    for (Thread t : taskThreads) {
      t.start();
    }

    // 使用 join() 方法,让 main 线程等待 5 个任务线程结束。
    for (Thread t : taskThreads) {
      try {
        t.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    System.out.printf("Main: Queue Size: %d\n", queue.size());

    for (int i = 0; i < taskThreads.length * 1000; i++) {
      Event event = queue.poll();
      System.out.printf("Thread %s: Priority %d\n", event.getThread(), event.getPriority());
    }

    System.out.printf("Main: Queue Size: %d\n", queue.size());
    System.out.printf("Main: End of the program\n");
  }