Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/AbstractExecutorService.java
          +++ new/src/share/classes/java/util/concurrent/AbstractExecutorService.java
↓ open down ↓ 43 lines elided ↑ open up ↑
  44   44   * to the {@link FutureTask} class provided in this package.  For example,
  45   45   * the implementation of <tt>submit(Runnable)</tt> creates an
  46   46   * associated <tt>RunnableFuture</tt> that is executed and
  47   47   * returned. Subclasses may override the <tt>newTaskFor</tt> methods
  48   48   * to return <tt>RunnableFuture</tt> implementations other than
  49   49   * <tt>FutureTask</tt>.
  50   50   *
  51   51   * <p> <b>Extension example</b>. Here is a sketch of a class
  52   52   * that customizes {@link ThreadPoolExecutor} to use
  53   53   * a <tt>CustomTask</tt> class instead of the default <tt>FutureTask</tt>:
  54      - * <pre>
       54 + *  <pre> {@code
  55   55   * public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
  56   56   *
  57      - *   static class CustomTask&lt;V&gt; implements RunnableFuture&lt;V&gt; {...}
       57 + *   static class CustomTask<V> implements RunnableFuture<V> {...}
  58   58   *
  59      - *   protected &lt;V&gt; RunnableFuture&lt;V&gt; newTaskFor(Callable&lt;V&gt; c) {
  60      - *       return new CustomTask&lt;V&gt;(c);
       59 + *   protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
       60 + *       return new CustomTask<V>(c);
  61   61   *   }
  62      - *   protected &lt;V&gt; RunnableFuture&lt;V&gt; newTaskFor(Runnable r, V v) {
  63      - *       return new CustomTask&lt;V&gt;(r, v);
       62 + *   protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
       63 + *       return new CustomTask<V>(r, v);
  64   64   *   }
  65   65   *   // ... add constructors, etc.
  66      - * }
  67      - * </pre>
       66 + * }}</pre>
       67 + *
  68   68   * @since 1.5
  69   69   * @author Doug Lea
  70   70   */
  71   71  public abstract class AbstractExecutorService implements ExecutorService {
  72   72  
  73   73      /**
  74   74       * Returns a <tt>RunnableFuture</tt> for the given runnable and default
  75   75       * value.
  76   76       *
  77   77       * @param runnable the runnable task being wrapped
↓ open down ↓ 21 lines elided ↑ open up ↑
  99   99      protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
 100  100          return new FutureTask<T>(callable);
 101  101      }
 102  102  
 103  103      /**
 104  104       * @throws RejectedExecutionException {@inheritDoc}
 105  105       * @throws NullPointerException       {@inheritDoc}
 106  106       */
 107  107      public Future<?> submit(Runnable task) {
 108  108          if (task == null) throw new NullPointerException();
 109      -        RunnableFuture<Object> ftask = newTaskFor(task, null);
      109 +        RunnableFuture<Void> ftask = newTaskFor(task, null);
 110  110          execute(ftask);
 111  111          return ftask;
 112  112      }
 113  113  
 114  114      /**
 115  115       * @throws RejectedExecutionException {@inheritDoc}
 116  116       * @throws NullPointerException       {@inheritDoc}
 117  117       */
 118  118      public <T> Future<T> submit(Runnable task, T result) {
 119  119          if (task == null) throw new NullPointerException();
↓ open down ↓ 31 lines elided ↑ open up ↑
 151  151          // For efficiency, especially in executors with limited
 152  152          // parallelism, check to see if previously submitted tasks are
 153  153          // done before submitting more of them. This interleaving
 154  154          // plus the exception mechanics account for messiness of main
 155  155          // loop.
 156  156  
 157  157          try {
 158  158              // Record exceptions so that if we fail to obtain any
 159  159              // result, we can throw the last exception we got.
 160  160              ExecutionException ee = null;
 161      -            long lastTime = (timed)? System.nanoTime() : 0;
      161 +            long lastTime = timed ? System.nanoTime() : 0;
 162  162              Iterator<? extends Callable<T>> it = tasks.iterator();
 163  163  
 164  164              // Start one task for sure; the rest incrementally
 165  165              futures.add(ecs.submit(it.next()));
 166  166              --ntasks;
 167  167              int active = 1;
 168  168  
 169  169              for (;;) {
 170  170                  Future<T> f = ecs.poll();
 171  171                  if (f == null) {
↓ open down ↓ 12 lines elided ↑ open up ↑
 184  184                          nanos -= now - lastTime;
 185  185                          lastTime = now;
 186  186                      }
 187  187                      else
 188  188                          f = ecs.take();
 189  189                  }
 190  190                  if (f != null) {
 191  191                      --active;
 192  192                      try {
 193  193                          return f.get();
 194      -                    } catch (InterruptedException ie) {
 195      -                        throw ie;
 196  194                      } catch (ExecutionException eex) {
 197  195                          ee = eex;
 198  196                      } catch (RuntimeException rex) {
 199  197                          ee = new ExecutionException(rex);
 200  198                      }
 201  199                  }
 202  200              }
 203  201  
 204  202              if (ee == null)
 205  203                  ee = new ExecutionException();
↓ open down ↓ 106 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX