emul/compact/src/main/java/java/util/concurrent/Executor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:51:24 +0200
branchjdk7-b147
changeset 1258 724f3e1ea53e
permissions -rw-r--r--
Additional set of classes to make porting of lookup library more easier
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     3
 *
jaroslav@1258
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
     9
 *
jaroslav@1258
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    14
 * accompanied this code).
jaroslav@1258
    15
 *
jaroslav@1258
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    19
 *
jaroslav@1258
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    22
 * questions.
jaroslav@1258
    23
 */
jaroslav@1258
    24
jaroslav@1258
    25
/*
jaroslav@1258
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1258
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1258
    28
 * However, the following notice accompanied the original version of this
jaroslav@1258
    29
 * file:
jaroslav@1258
    30
 *
jaroslav@1258
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1258
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1258
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1258
    34
 */
jaroslav@1258
    35
jaroslav@1258
    36
package java.util.concurrent;
jaroslav@1258
    37
jaroslav@1258
    38
/**
jaroslav@1258
    39
 * An object that executes submitted {@link Runnable} tasks. This
jaroslav@1258
    40
 * interface provides a way of decoupling task submission from the
jaroslav@1258
    41
 * mechanics of how each task will be run, including details of thread
jaroslav@1258
    42
 * use, scheduling, etc.  An <tt>Executor</tt> is normally used
jaroslav@1258
    43
 * instead of explicitly creating threads. For example, rather than
jaroslav@1258
    44
 * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
jaroslav@1258
    45
 * of a set of tasks, you might use:
jaroslav@1258
    46
 *
jaroslav@1258
    47
 * <pre>
jaroslav@1258
    48
 * Executor executor = <em>anExecutor</em>;
jaroslav@1258
    49
 * executor.execute(new RunnableTask1());
jaroslav@1258
    50
 * executor.execute(new RunnableTask2());
jaroslav@1258
    51
 * ...
jaroslav@1258
    52
 * </pre>
jaroslav@1258
    53
 *
jaroslav@1258
    54
 * However, the <tt>Executor</tt> interface does not strictly
jaroslav@1258
    55
 * require that execution be asynchronous. In the simplest case, an
jaroslav@1258
    56
 * executor can run the submitted task immediately in the caller's
jaroslav@1258
    57
 * thread:
jaroslav@1258
    58
 *
jaroslav@1258
    59
 * <pre>
jaroslav@1258
    60
 * class DirectExecutor implements Executor {
jaroslav@1258
    61
 *     public void execute(Runnable r) {
jaroslav@1258
    62
 *         r.run();
jaroslav@1258
    63
 *     }
jaroslav@1258
    64
 * }</pre>
jaroslav@1258
    65
 *
jaroslav@1258
    66
 * More typically, tasks are executed in some thread other
jaroslav@1258
    67
 * than the caller's thread.  The executor below spawns a new thread
jaroslav@1258
    68
 * for each task.
jaroslav@1258
    69
 *
jaroslav@1258
    70
 * <pre>
jaroslav@1258
    71
 * class ThreadPerTaskExecutor implements Executor {
jaroslav@1258
    72
 *     public void execute(Runnable r) {
jaroslav@1258
    73
 *         new Thread(r).start();
jaroslav@1258
    74
 *     }
jaroslav@1258
    75
 * }</pre>
jaroslav@1258
    76
 *
jaroslav@1258
    77
 * Many <tt>Executor</tt> implementations impose some sort of
jaroslav@1258
    78
 * limitation on how and when tasks are scheduled.  The executor below
jaroslav@1258
    79
 * serializes the submission of tasks to a second executor,
jaroslav@1258
    80
 * illustrating a composite executor.
jaroslav@1258
    81
 *
jaroslav@1258
    82
 *  <pre> {@code
jaroslav@1258
    83
 * class SerialExecutor implements Executor {
jaroslav@1258
    84
 *   final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
jaroslav@1258
    85
 *   final Executor executor;
jaroslav@1258
    86
 *   Runnable active;
jaroslav@1258
    87
 *
jaroslav@1258
    88
 *   SerialExecutor(Executor executor) {
jaroslav@1258
    89
 *     this.executor = executor;
jaroslav@1258
    90
 *   }
jaroslav@1258
    91
 *
jaroslav@1258
    92
 *   public synchronized void execute(final Runnable r) {
jaroslav@1258
    93
 *     tasks.offer(new Runnable() {
jaroslav@1258
    94
 *       public void run() {
jaroslav@1258
    95
 *         try {
jaroslav@1258
    96
 *           r.run();
jaroslav@1258
    97
 *         } finally {
jaroslav@1258
    98
 *           scheduleNext();
jaroslav@1258
    99
 *         }
jaroslav@1258
   100
 *       }
jaroslav@1258
   101
 *     });
jaroslav@1258
   102
 *     if (active == null) {
jaroslav@1258
   103
 *       scheduleNext();
jaroslav@1258
   104
 *     }
jaroslav@1258
   105
 *   }
jaroslav@1258
   106
 *
jaroslav@1258
   107
 *   protected synchronized void scheduleNext() {
jaroslav@1258
   108
 *     if ((active = tasks.poll()) != null) {
jaroslav@1258
   109
 *       executor.execute(active);
jaroslav@1258
   110
 *     }
jaroslav@1258
   111
 *   }
jaroslav@1258
   112
 * }}</pre>
jaroslav@1258
   113
 *
jaroslav@1258
   114
 * The <tt>Executor</tt> implementations provided in this package
jaroslav@1258
   115
 * implement {@link ExecutorService}, which is a more extensive
jaroslav@1258
   116
 * interface.  The {@link ThreadPoolExecutor} class provides an
jaroslav@1258
   117
 * extensible thread pool implementation. The {@link Executors} class
jaroslav@1258
   118
 * provides convenient factory methods for these Executors.
jaroslav@1258
   119
 *
jaroslav@1258
   120
 * <p>Memory consistency effects: Actions in a thread prior to
jaroslav@1258
   121
 * submitting a {@code Runnable} object to an {@code Executor}
jaroslav@1258
   122
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
jaroslav@1258
   123
 * its execution begins, perhaps in another thread.
jaroslav@1258
   124
 *
jaroslav@1258
   125
 * @since 1.5
jaroslav@1258
   126
 * @author Doug Lea
jaroslav@1258
   127
 */
jaroslav@1258
   128
public interface Executor {
jaroslav@1258
   129
jaroslav@1258
   130
    /**
jaroslav@1258
   131
     * Executes the given command at some time in the future.  The command
jaroslav@1258
   132
     * may execute in a new thread, in a pooled thread, or in the calling
jaroslav@1258
   133
     * thread, at the discretion of the <tt>Executor</tt> implementation.
jaroslav@1258
   134
     *
jaroslav@1258
   135
     * @param command the runnable task
jaroslav@1258
   136
     * @throws RejectedExecutionException if this task cannot be
jaroslav@1258
   137
     * accepted for execution.
jaroslav@1258
   138
     * @throws NullPointerException if command is null
jaroslav@1258
   139
     */
jaroslav@1258
   140
    void execute(Runnable command);
jaroslav@1258
   141
}