emul/compact/src/main/java/java/util/concurrent/Executor.java
branchjdk7-b147
changeset 1258 724f3e1ea53e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/util/concurrent/Executor.java	Sat Sep 07 13:51:24 2013 +0200
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.6 + *
     1.7 + * This code is free software; you can redistribute it and/or modify it
     1.8 + * under the terms of the GNU General Public License version 2 only, as
     1.9 + * published by the Free Software Foundation.  Oracle designates this
    1.10 + * particular file as subject to the "Classpath" exception as provided
    1.11 + * by Oracle in the LICENSE file that accompanied this code.
    1.12 + *
    1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 + * version 2 for more details (a copy is included in the LICENSE file that
    1.17 + * accompanied this code).
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License version
    1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 + *
    1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 + * or visit www.oracle.com if you need additional information or have any
    1.25 + * questions.
    1.26 + */
    1.27 +
    1.28 +/*
    1.29 + * This file is available under and governed by the GNU General Public
    1.30 + * License version 2 only, as published by the Free Software Foundation.
    1.31 + * However, the following notice accompanied the original version of this
    1.32 + * file:
    1.33 + *
    1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
    1.35 + * Expert Group and released to the public domain, as explained at
    1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
    1.37 + */
    1.38 +
    1.39 +package java.util.concurrent;
    1.40 +
    1.41 +/**
    1.42 + * An object that executes submitted {@link Runnable} tasks. This
    1.43 + * interface provides a way of decoupling task submission from the
    1.44 + * mechanics of how each task will be run, including details of thread
    1.45 + * use, scheduling, etc.  An <tt>Executor</tt> is normally used
    1.46 + * instead of explicitly creating threads. For example, rather than
    1.47 + * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
    1.48 + * of a set of tasks, you might use:
    1.49 + *
    1.50 + * <pre>
    1.51 + * Executor executor = <em>anExecutor</em>;
    1.52 + * executor.execute(new RunnableTask1());
    1.53 + * executor.execute(new RunnableTask2());
    1.54 + * ...
    1.55 + * </pre>
    1.56 + *
    1.57 + * However, the <tt>Executor</tt> interface does not strictly
    1.58 + * require that execution be asynchronous. In the simplest case, an
    1.59 + * executor can run the submitted task immediately in the caller's
    1.60 + * thread:
    1.61 + *
    1.62 + * <pre>
    1.63 + * class DirectExecutor implements Executor {
    1.64 + *     public void execute(Runnable r) {
    1.65 + *         r.run();
    1.66 + *     }
    1.67 + * }</pre>
    1.68 + *
    1.69 + * More typically, tasks are executed in some thread other
    1.70 + * than the caller's thread.  The executor below spawns a new thread
    1.71 + * for each task.
    1.72 + *
    1.73 + * <pre>
    1.74 + * class ThreadPerTaskExecutor implements Executor {
    1.75 + *     public void execute(Runnable r) {
    1.76 + *         new Thread(r).start();
    1.77 + *     }
    1.78 + * }</pre>
    1.79 + *
    1.80 + * Many <tt>Executor</tt> implementations impose some sort of
    1.81 + * limitation on how and when tasks are scheduled.  The executor below
    1.82 + * serializes the submission of tasks to a second executor,
    1.83 + * illustrating a composite executor.
    1.84 + *
    1.85 + *  <pre> {@code
    1.86 + * class SerialExecutor implements Executor {
    1.87 + *   final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
    1.88 + *   final Executor executor;
    1.89 + *   Runnable active;
    1.90 + *
    1.91 + *   SerialExecutor(Executor executor) {
    1.92 + *     this.executor = executor;
    1.93 + *   }
    1.94 + *
    1.95 + *   public synchronized void execute(final Runnable r) {
    1.96 + *     tasks.offer(new Runnable() {
    1.97 + *       public void run() {
    1.98 + *         try {
    1.99 + *           r.run();
   1.100 + *         } finally {
   1.101 + *           scheduleNext();
   1.102 + *         }
   1.103 + *       }
   1.104 + *     });
   1.105 + *     if (active == null) {
   1.106 + *       scheduleNext();
   1.107 + *     }
   1.108 + *   }
   1.109 + *
   1.110 + *   protected synchronized void scheduleNext() {
   1.111 + *     if ((active = tasks.poll()) != null) {
   1.112 + *       executor.execute(active);
   1.113 + *     }
   1.114 + *   }
   1.115 + * }}</pre>
   1.116 + *
   1.117 + * The <tt>Executor</tt> implementations provided in this package
   1.118 + * implement {@link ExecutorService}, which is a more extensive
   1.119 + * interface.  The {@link ThreadPoolExecutor} class provides an
   1.120 + * extensible thread pool implementation. The {@link Executors} class
   1.121 + * provides convenient factory methods for these Executors.
   1.122 + *
   1.123 + * <p>Memory consistency effects: Actions in a thread prior to
   1.124 + * submitting a {@code Runnable} object to an {@code Executor}
   1.125 + * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
   1.126 + * its execution begins, perhaps in another thread.
   1.127 + *
   1.128 + * @since 1.5
   1.129 + * @author Doug Lea
   1.130 + */
   1.131 +public interface Executor {
   1.132 +
   1.133 +    /**
   1.134 +     * Executes the given command at some time in the future.  The command
   1.135 +     * may execute in a new thread, in a pooled thread, or in the calling
   1.136 +     * thread, at the discretion of the <tt>Executor</tt> implementation.
   1.137 +     *
   1.138 +     * @param command the runnable task
   1.139 +     * @throws RejectedExecutionException if this task cannot be
   1.140 +     * accepted for execution.
   1.141 +     * @throws NullPointerException if command is null
   1.142 +     */
   1.143 +    void execute(Runnable command);
   1.144 +}