/** * Waits until the thread is not paused, and there's indication that it has * something to do */ private synchronized void waitForUnpause() { final String METHOD_NAME = "wait_for_unpause()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { while (!getDone().get() && (getWaitWhenDone().get() || (getEventsToProcess().isEmpty() && getDirtyUnits().isEmpty()))) { getOwner().log(getClass(), METHOD_NAME, "waitWhenDone = " + getWaitWhenDone() + " :: eventsToProcess = " + getEventsToProcess().size() + " :: dirtyUnits = " + getDirtyUnits().size()); getWaiting().set(true); try { wait(); } catch (InterruptedException ignored) { } // System.err.println("checking WAIT conditions"); } getWaiting().set(false); } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
public void run() { final String METHOD_NAME = "run()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { // todo There's probably a better way to handle this than a loop that only exits on an error. //noinspection InfiniteLoopStatement while (!getDone().get()) { if (!getEventsToProcess().isEmpty()) { processGameEvents(); ecmInfo = ComputeECM.computeAllEntitiesECMInfo( getGame().getEntitiesVector()); } else if (!getDirtyUnits().isEmpty()) { Entity entity = getGame().getEntity(getDirtyUnits().pollFirst()); if ((entity != null) && isEntityOnMap(entity)) { unPause(); getOwner().log(getClass(), METHOD_NAME, "recalculating paths for " + entity.getDisplayName()); getPathEnumerator().recalculateMovesFor(entity); getOwner().log(getClass(), METHOD_NAME, "finished recalculating paths for " + entity .getDisplayName()); } } else if (getWaitWhenDone().get()) { waitForUnpause(); // paused for a reason } else { waitForUnpause(); // idling because there's nothing to do } } } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
LinkedList<GameEvent> eventsToProcessIterator = new LinkedList<>(getEventsToProcess()); int numEvents = eventsToProcessIterator.size(); for (int count = 0; count < numEvents; count++) { getEventsToProcess().remove(event); if (event instanceof GameEntityChangeEvent) { getOwner().log(getClass(), METHOD_NAME, "Events still to process: " + getEventsToProcess().size()); } finally { GAME_LOCK.unlock();