0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent;
0N/A
0N/A/**
0N/A * An object that executes submitted {@link Runnable} tasks. This
0N/A * interface provides a way of decoupling task submission from the
0N/A * mechanics of how each task will be run, including details of thread
0N/A * use, scheduling, etc. An <tt>Executor</tt> is normally used
0N/A * instead of explicitly creating threads. For example, rather than
0N/A * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
0N/A * of a set of tasks, you might use:
0N/A *
0N/A * <pre>
0N/A * Executor executor = <em>anExecutor</em>;
0N/A * executor.execute(new RunnableTask1());
0N/A * executor.execute(new RunnableTask2());
0N/A * ...
0N/A * </pre>
0N/A *
0N/A * However, the <tt>Executor</tt> interface does not strictly
0N/A * require that execution be asynchronous. In the simplest case, an
0N/A * executor can run the submitted task immediately in the caller's
0N/A * thread:
0N/A *
0N/A * <pre>
0N/A * class DirectExecutor implements Executor {
0N/A * public void execute(Runnable r) {
0N/A * r.run();
0N/A * }
0N/A * }</pre>
0N/A *
0N/A * More typically, tasks are executed in some thread other
0N/A * than the caller's thread. The executor below spawns a new thread
0N/A * for each task.
0N/A *
0N/A * <pre>
0N/A * class ThreadPerTaskExecutor implements Executor {
0N/A * public void execute(Runnable r) {
0N/A * new Thread(r).start();
0N/A * }
0N/A * }</pre>
0N/A *
0N/A * Many <tt>Executor</tt> implementations impose some sort of
0N/A * limitation on how and when tasks are scheduled. The executor below
0N/A * serializes the submission of tasks to a second executor,
0N/A * illustrating a composite executor.
0N/A *
3203N/A * <pre> {@code
0N/A * class SerialExecutor implements Executor {
3203N/A * final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
3203N/A * final Executor executor;
3203N/A * Runnable active;
0N/A *
3203N/A * SerialExecutor(Executor executor) {
3203N/A * this.executor = executor;
3203N/A * }
0N/A *
3203N/A * public synchronized void execute(final Runnable r) {
3203N/A * tasks.offer(new Runnable() {
3203N/A * public void run() {
3203N/A * try {
3203N/A * r.run();
3203N/A * } finally {
3203N/A * scheduleNext();
0N/A * }
3203N/A * }
3203N/A * });
3203N/A * if (active == null) {
3203N/A * scheduleNext();
0N/A * }
3203N/A * }
0N/A *
3203N/A * protected synchronized void scheduleNext() {
3203N/A * if ((active = tasks.poll()) != null) {
3203N/A * executor.execute(active);
0N/A * }
3203N/A * }
3203N/A * }}</pre>
0N/A *
0N/A * The <tt>Executor</tt> implementations provided in this package
0N/A * implement {@link ExecutorService}, which is a more extensive
0N/A * interface. The {@link ThreadPoolExecutor} class provides an
0N/A * extensible thread pool implementation. The {@link Executors} class
0N/A * provides convenient factory methods for these Executors.
0N/A *
0N/A * <p>Memory consistency effects: Actions in a thread prior to
0N/A * submitting a {@code Runnable} object to an {@code Executor}
0N/A * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
0N/A * its execution begins, perhaps in another thread.
0N/A *
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A */
0N/Apublic interface Executor {
0N/A
0N/A /**
0N/A * Executes the given command at some time in the future. The command
0N/A * may execute in a new thread, in a pooled thread, or in the calling
0N/A * thread, at the discretion of the <tt>Executor</tt> implementation.
0N/A *
0N/A * @param command the runnable task
0N/A * @throws RejectedExecutionException if this task cannot be
0N/A * accepted for execution.
0N/A * @throws NullPointerException if command is null
0N/A */
0N/A void execute(Runnable command);
0N/A}