/** Unblocks all threads and runs all listeners. */ private void complete() { for (Waiter currentWaiter = clearWaiters(); currentWaiter != null; currentWaiter = currentWaiter.next) { currentWaiter.unpark(); } // We need to reverse the list to handle buggy listeners that depend on ordering. Listener currentListener = clearListeners(); Listener reversedList = null; while (currentListener != null) { Listener tmp = currentListener; currentListener = currentListener.next; tmp.next = reversedList; reversedList = tmp; } for (; reversedList != null; reversedList = reversedList.next) { executeListener(reversedList.task, reversedList.executor); } // We call this after the listeners on the theory that done() will only be used for 'cleanup' // oriented tasks (e.g. clearing fields) and so can wait behind listeners which may be executing // more important work. A counter argument would be that done() is trusted code and therefore // it would be safe to run before potentially slow or poorly behaved listeners. Reevaluate this // once we have more examples of done() implementations. done(); }