2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
22 /* ------------------------------------------------------------ */
23 /** Iterating Nested Callback.
24 * <p>This specialized callback is used when breaking up an
25 * asynchronous task into smaller asynchronous tasks. A typical pattern
26 * is that a successful callback is used to schedule the next sub task, but
27 * if that task completes quickly and uses the calling thread to callback
28 * the success notification, this can result in a growing stack depth.
30 * <p>To avoid this issue, this callback uses an AtomicBoolean to note
31 * if the success callback has been called during the processing of a
32 * sub task, and if so then the processing iterates rather than recurses.
34 * <p>This callback is passed to the asynchronous handling of each sub
35 * task and a call the {@link #succeeded()} on this call back represents
36 * completion of the subtask. Only once all the subtasks are completed is
37 * the {@link Callback#succeeded()} method called on the {@link Callback} instance
38 * passed the the {@link #IteratingNestedCallback(Callback)} constructor.</p>
41 public abstract class IteratingNestedCallback extends IteratingCallback
43 final Callback _callback;
45 public IteratingNestedCallback(Callback callback)
51 protected void completed()
53 _callback.succeeded();
57 public void failed(Throwable x)
64 public String toString()
66 return String.format("%s@%x",getClass().getSimpleName(),hashCode());